How to dynamically configure servlet settings of non-embedded Jetty? - jetty

I have a non-embedded Jetty. I would like to dynamically configure a servlet's settings. Specifically, I want to change the default servlet's resourceBase setting.
In the web.xml file it's simply
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>resourceBase</param-name>
<param-value>something</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
But how can I change this dynamically using code? And where do I have to put that code?

If you have a webapp, and are using WEB-INF/web.xml, then the resourceBase is the webapp itself.
You cannot change that.
You can, however, make a NEW DefaultServlet entry, to serve other content from a different url-pattern (note: You cannot use / or /* for this new servlet's url-pattern), just define it as a new Servlet, with it's own name.

Related

unable to add additional APIs endpoint classes to GCP endpoints v2

I have started an Google cloud endpoints v2 project using the starter code provided by Google which exposes one API named "echo".
My aim is to add another exposed class (one class/api name per business module).
Once deployed, all calls to the new API are not generating a "NOT FOUND" error.
As a solution i have tried to look into the web.xml and did the below changes.
Initial WEB.XML version:
<!-- Route API method requests to the backend. -->
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/*</url-pattern>
</servlet-mapping>
I have added my new servlet and tried to add a new servlet mapping as below:
<servlet-mapping>
<servlet-name>EndpointsServlet</servlet-name>
<url-pattern>/_ah/api/echo/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserEndpointsServlet</servlet-name>
<url-pattern>/_ah/api/user/*</url-pattern>
</servlet-mapping>
Now after adding the /echo/* to the initial servlet mapping, the ECHO working services stopped servicing, and the system still cannot invoke the new service.
Does anyone knows what i am doing wrong and what is the solution?
I don't think this feature is not supported by the endpoints v2 framework since it does seem a good design practice to do this separation.
You should use the same servlet, using an init-param with all of the Endpoint classes you need:
<init-param>
<param-name>services</param-name>
<param-value>com.example.echo.Echo,com.example.echo.Echo2</param-value>
</init-param>
You should not use /_ah/api/echo/* or /_ah/api/user/* in your web.xml. Instead, use #Api(name = "echo") or #Api(name = "user") and bind EndpointsServlet to /_ah/api/*.

Using Jersey 2.5 on Websphere 8.X

I am using the following configurations for my Rest-WS application and it deploys/works well on WAS 7.x.
But when i deploy the same on WAS8.0, i get the following exception:
(java.lang.NoSuchMethodError:
javax/ws/rs/core/Application.getProperties()Ljava/util/Map)
I understand that WAS 8.X has a JAX-RS-1.0 runtime and since the 1.0 api interface does not have the Application::getProperties(), i get the exception above.
So how do i get my project to work on WAS 8.X. Any pointers would be much appreciated..
My current application configuration as below -
JAX-RS-2.0/JSR339
JERSEY 2.5
JACKSON 2.3
WEB.XML
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.packagename.resources.MyAppResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
MyAppResourceConfig.java
public class MyAppResourceConfig extends ResourceConfig {
public MonitoringResourceConfig() {
// Registers JAX-RS Components
register(RequestContextFilter.class);
register(MyRestResource.class);
packages( "com.packagename");
}
}
MyRestResource.java contains #path definitions.
P.S - I tried setting the JVM property DisableIBMJAXRSEngine in WAS, but it doesn't help.
I'm having the same trouble with Jersey 2.6 and WAS 8.0.0.3. I tried changing the class loading policy to parent last (on both, web module and application) and putting the Jersey jars into the application. It solved that problem, but is too restricted for my context.

Starting JAXWS servlet at root of the web context?

Is it possible to start JAXWS servlet at the roor of the web contex, so that the WSDL will be also accessible?
My context root, defined in application.xml is /myapp/webservice/v1 (the last part should be configurable in the deployment descriptor, and the web service should be available in web context root).
I've tried:
<servlet>
<display-name>WebService</display-name>
<servlet-name>WebService</servlet-name>
<servlet-class>my.package.WebServiceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WebService</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
The WebService answers under the address /myapp/webservice/v1, but when I type /myapp/webservice/v1?wsdl, it's converted into /myapp/webservice/v1/?wsdl, which doesn't return WSDL, but it tries to invoke the WebService itself, which answers: "Hello! This is an Axis2 Web Service!" (standard greeting when doing GET to WebService).
I've tried also with url pattern '.'. '' and '/' are not accepted (WebSphere).
When I use
<url-pattern>ws</url-pattern>
and
<welcome-file-list>
<welcome-file>ws</welcome-file>
</welcome-file-list>
the web service works, but the WSDL is available only on /myapp/webservice/v1/ws?wsdl, which makes the solution incompatibile with existing clients.
The reason behind is to create version as separate WARs, which can be then deployed independently or within EAR, and to make WebService address fully configurable, we need to make it accessible under the context root of the Web Module.
I don't know if the problems we are experiencing are WebSphere-specific or Axis-specific...

why is my Cloundfoundry application showing JSP files as source, not rendered?

I have created a cloundfoundry app using Spring. However, my jsp files are being rendered as pure text (as if Tomcat is not executing the source code). The browser shows the source when requesting a particular URL.
File structure
webapps
-jsp
-javascripts
-css
-WEB-INF
web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles Spring requests -->
<servlet>
<servlet-name>Honesty</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Honesty</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
spring config:
<!-- Turns on support for mapping requests to Spring MVC #Controller methods
Also registers default Formatters and Validators for use across all #Controllers -->
<mvc:annotation-driven/>
<context:annotation-config />
<mvc:default-servlet-handler/>
Thanks for any insight you can provide. Its as if Tomcat is not serving the request as the JSP source is showing, not a JSP rendered page.
The response headers show nginx as the server:
Connection:keep-alive
Date:Mon, 30 Jul 2012 03:58:16 GMT
ETag:W/"611-1343620259000"
Keep-Alive:timeout=20
Server:nginx
Are you using Spring MVC? I suppose you are putting the JSP in some resource directory which can be accessed. Would like to see your spring configuration file and your project structure.
The problem was my servlet mapping of "/*". switched to just "/" and now everything works as expected.

Redirect / to /index.jsp when deployed to root

I am struggling with a webapp deployment in Jetty 6. Previously the the webapp was deployed in /mywebapp and everytime I accessed http://localhost/mywebapp/ Jetty directed me to http://localhost/mywebapp/index.jsp.
When I change the contextPath to /, suddenly the redirect behaviour is broken. Instead, Jetty makes an internal forward request.
Does anyone have any input why this is happening? The DefaultServlet has the following settings:
<init-param>
<param-name>dirAllowed</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>welcomeServlets</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>redirectWelcome</param-name>
<param-value>true</param-value>
</init-param>
Turns out it was not exactly the change of contextPath that made the redirects fail. Instead, I realized that simply the action of adding mywebapp.xml into $JETTY_HOME/contexts/ made Jetty not execute the redirects.
I overcame this issue by deleting my context XML file, instead renaming $JETTY_HOME/webapps/mywebapp.war to $JETTY_HOME/webapps/root.war solved the issue.
Unclear why this happened in the first place, but I'm happy things are back on track.