set init params to jsp page without web.xml - java

I know that I can set init params for a jsp page using web.xml. For example
<servlet>
<servlet-name>init</servlet-name>
<jsp-file>/init.jsp</jsp-file>
<init-param>
<param-name>test</param-name>
<param-value>me</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>init</servlet-name>
<url-pattern>/init.jsp</url-pattern>
</servlet-mapping>
then in my init.jsp I can get the init params using getServletContext.getInitParameter..
However, I am using WebSevlet 3.0 annotations and I can't see equivalent jsp-file annotation. I am looking for something along these lines
#WebServlet(initParams=#WebInitParam(name="hello", value = "hello"),description = "A Simple Servlet", urlPatterns = { "/init.jsp" })
where I can use jsp-file annotation. So I need to set the jsp init params without using web.xml.

I know you said without using a web.xml - but you could still use a web.xml file with annotations - and just use it for the parameters - define the init params as a context-param. That would set the parameter for the whole application...
<context-param>
<param-name>someParameter</param-name>
<param-value>someValue</param-value>
</context-param>
Then in your code - reference it in the same way as above :
getServletContext.getInitParameter("someParameter");

Related

Struts to spring migration getting 404 error

/spring/fetchAllUsers URL which am trying
web.xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext/dataSource.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/spring/</url-pattern>
</servlet-mapping>
Controller Code
#RequestMapping(value = "/getAllUsers", method = RequestMethod.POST)
#ResponseBody
#Transactional(propagation=Propogation.REQUIRED,rollBackFor=Exception.class)
public String fetchAllUsers(){
setInputStream(userDelegate.fetchAllUsers());
return success;
Details:
And I have mvc annotation driven and mvc default servlet handler in user-servlet.xml
Getting 404 error code when try to access this URl when doing migration from struts to spring
Break point is not hit when this URL is hit and no errors in console as well.Please suggest on the above issue
According to your servlet mapping only one url is allowed localhost:8080/context/spring/ that is not mapped with your controller.
When we defined a servlet-mapping, we are using SimpleUrlHandlerMapping. To understand servlet url mapping let define a servlet mapping :
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
Now the handler will actually use the * part to find the controller. It will not search /spring/createUser, it will only search for /createUser to find a corresponding Controller.
#RequestMapping("/createUser")
In your case You need to either change your url to localhost:8080/spring/spring/createUser or remove prefix from Controller #RequestingMapping(/createUser).
since your servlet url mapping has already include /spring you don't need to include it in #RequestMapping.
try this:
#RequestMapping(value = "/createUser/", method = RequestMethod.POST)
Spring interprets urls /spring/createUser/ and /spring/createUser differently(atleast in POST methods that i just tested).
Change your #RequestMapping url to /spring/createUser.
Also mind that the url you call is /spring/createUser without a trailing slash("/").
Your method
#RequestMapping(value = "/spring/createUser", method = RequestMethod.POST)
Hope this helps.

#PathVariable is not working in spring

I have PersonController as below :
#Controller
#RequestMapping("person")
public class PersonController {
#RequestMapping(value= "/{personId}", method = RequestMethod.GET, produces={"application/json"})
public #ResponseBody Map<String, Object> getPerson(#PathVariable("personId") Integer personId) {
// code to get person
}
Tomcat starts up fine, I see this in the console :
Mapped "{[/person/{personId}],methods=[GET],params=[],headers=[] ,consumes=[],produces=[application/json],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.test.web.controller.PersonController.getPerson(java.lang.Integer)
But if I hit the url http://localhost:8080/sample/person/1 I get
HTTP Status 404 - /sample/person/1
In the web.xml
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/sample/*</url-pattern>
</servlet-mapping>
I copy/pasted your PersonController class and it worked fine here.
So I did check your web.xml and your app servlet is mapping the pattern "/sample/*".
If I am corret, I suspect your project is called "sample" in Eclipse. In that case, you have to access your site as follows:
http://localhost:8080/sample/sample/person/1
The mapping in your web.xml will always start from your root context, and that is why you are getting 404 error.
If you want to access your controller from the root domain (in this case it is your actual Eclipse project name by default, but it can be configured too) you can use your servlet mapping as follows:
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I recommend that you use /rest/* or other mark since it will scale better for other types of content.
Let me know if it worked.

Adding multiple servlets in single web.xml

I am trying to run two Servlet-class in a single web.xml but its not working, each servlet-class works fine independently.
web.xml:
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>240</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-ws-servlet.xml
/WEB-INF/health-page-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>health-page</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>health-page</servlet-name>
<url-pattern>/health.htm</url-pattern>
</servlet-mapping>
Do let me know if you can figure something wrong that i am doing.
I tried the below link but it doesnt work for me
Can I use Spring MVC and Spring WS in one single application?
This isn't going to work. The one which is mapped on /* overtakes all requests. You need to map it on / instead so that it will only intercept on requests which are not matched by all other existing servlets (including the JSP servlet which is implicitly mapped on *.jsp and all "normal" static resources like CSS/JS/image files!). See also Difference between / and /* in servlet mapping url pattern.
If being able to serve static resources is also required, then better map it on a more specific URL pattern like /ws/* and create a Filter which checks the request URI and then forwards accordingly. That filter can in turn safely be mapped on /*. See also this answer for a more concrete code example: How to access static resources when mapping a global front controller servlet on /*.
I am using Java configuration in my project and following code works fine for the same purpose:
public class Initializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfiguration.class);
ctx.setServletContext(servletContext);
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(ctx);
messageDispatcherServlet.setTransformWsdlLocations(true);
Dynamic dynamic = servletContext.addServlet("messageDispatcherServlet", messageDispatcherServlet);
dynamic.addMapping("/ws/*");
dynamic.setLoadOnStartup(1);
dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
}
}
you have a mapping for /* in the spring-ws section which is getting the request. you need to
come up with a different strategy... Try putting the /health.htm before the /* mapping.

How to allow access to static content when having default servlet

I map all requests to /* to a specific servlet.
My static content is hidden by this configuration.
How can i allow access to specific files (such as crossdomain.xml)?
When you map /* to a specific servlet, all requests will be forwarded to that servlet, unless you provide a more explicit mapping to another servlet.
That is, if you have /* mapped to ServletA, and /static/* mapped to ServletB, then following Servlets will get called.
http://localhost:8080/abc.jpg -> ServletA
http://localhost:8080/static/abc.jpg -> ServletB
http://localhost:8080/xyz/abc.jpg -> ServletA
So one option you have is to write a Servlet to handle the static content, which will grab the file and return it as response. You can map that servlet to a prefixed by something like /static/*. This requires that all URL references to your static files to be updated to contain this '/static' part.
If that is not feasible for you, then probably you can use the same servlet, but mapped to multiple URL patterns (probably by extension) as follows.
<servlet>
<servlet-name>static-servlet</servlet-name>
<servlet-class>xxx.yyy.StaticServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>static-servlet</servlet-name>
<url-pattern>*.xml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>static-servlet</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
If you want this to be fine-grained to the level of each file, you can map the servlet to your file URL as well.
Cookbook:
Map your controller Servlet on a more specific url-pattern like /app/*.
Put all the static content in a specific folder like /static.
Create a Filter which is mapped on /* which transparently continues the chain for any /static requests and dispatches other requests to /app.
So, in a nutshell:
<filter>
<filter-name>filter</filter-name>
<filter-class>com.example.Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
with the following in filter's doFilter():
String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.startsWith("/static/")) {
chain.doFilter(request, response); // Goes to default servlet.
} else {
request.getRequestDispatcher("/app" + uri).forward(request, response);
}
No, you do not end up with extra /app path in the URL. It's fully transparent. Make if necessary "/static" and/or "/app" an <init-param> of the filter.
And one more(a direct) servlet mapping like this<servlet-mapping><servlet-name>StaticContentServlet</servlet-name><url-pattern>/crossdomain.xml</url-pattern></servlet-mapping>
probably you can put your static content under different URL like /static/* and then map this URL to a Servlet which responds with the static content.

Accessing context parameters within Servlet Filters

Thanks to everyone in advance,
I am trying to access any context parameters in the web.xml from within a servlet filter. Below is a portion from my web.xml file. I have verified that the context-param node is accessible via a jsp page using out.print(getServletContext().getInitParameter("error"));.
<filter>
<filter-name>prePost</filter-name>
<filter-class>MyFilter</filter-class>
<init_param>
<param_name>error</param_name>
<param_value>/test.jsp</param_value>
</init_param>
<filter-mapping>
<filter-name>prePost</filter-name>
<url-pattern>*.jsp</url-pattern>
<context-param>
<description>Error Handler</description>
<param-name>error</param-name>
<param-value>/test.jsp</param-value>
In my filters doFilter when I output this.filterConfig.getInitParameter("error"), I always get null. In my filters init() I am setting this.filterConfig with the passed in FilterConfig.
Thanks,
Sam
You're using underscores rather than hyphens for "param-name" and "param-value". Your config should look like this:
<init-param>
<param-name>error</param-name>
<param-value>/test.jsp</param-value>
</init-param>

Categories

Resources