How to configure session timeout, error pages programmatically without web.xml

I am using Spring MVC and have successfully configured WebApplicationInitializer (using Tomcat ServletContainerInitializer) without a web.xml file. Adding filters (e.g. Spring Security) and servlets (e.g. Dispatcher) is not a problem and they work fine. I can also install init-params if I need to do this.

I cannot figure out how to set up some special tags that usually exist in web.xml. For example, I would like to customize the 403 error page. Usually I did this in web.xml with:

<error-page> <error-code>403</error-code> <location>/accessDenied.html</location> </error-page> 

But I cannot figure out how to do this in the WebApplicationInitializer (which has access to the ServletContext).

I have the same problem with session-timeout and welcome files. I searched for about two days, but still have not seen this programmatically. Again, the goal is to completely remove the web.xml file and use the initializer class instead.

Any ideas?

+8
spring spring-mvc
source share
5 answers

It doesn't seem like it's possible through WebApplicationInitializer, you will need to stick to web.xml specifically for this configuration along with some of the others listed in this question - Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure the session configuration and error page

+7
source share
 StandardEngine standardEngine = (StandardEngine)MBeanServerFactory.findMBeanServer(null).get(0).getAttribute(new ObjectName("Catalina", "type", "Engine"), "managedResource"); // This is totally irresponsible and will only work if this webapp is running on the default host StandardContext standardContext = (StandardContext)standardEngine.findChild(standardEngine.getDefaultHost()).findChild(servletContext.getContextPath()); ErrorPage errorPage404 = new ErrorPage(); errorPage404.setErrorCode(404); errorPage404.setLocation("/404"); standardContext.addErrorPage(errorPage404); 
+2
source share

You can use something like this:

 @WebFilter(filterName = "ErrorPageFilter", urlPatterns = "/*") public class ErrorPageFilter extends BaseHttpFilter { private GlobalErrorHandler globalErrorHandler; @Override public void init(FilterConfig filterConfig) throws ServletException { globalErrorHandler = BeansUtil.getBean(filterConfig.getServletContext(), GlobalErrorHandler.class); } @Override protected void doHttpFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(request, new ErrorPageHttpServletResponseWrapper(request, response)); } private class ErrorPageHttpServletResponseWrapper extends HttpServletResponseWrapper { HttpServletRequest request; public ErrorPageHttpServletResponseWrapper(HttpServletRequest request, HttpServletResponse response) { super(response); this.request = request; } @Override public void sendError(int sc) throws IOException { globalErrorHandler.handleError(sc, request, this); } @Override public void sendError(int sc, String msg) throws IOException { sendError(sc); } } } public interface GlobalErrorHandler { void handleError(int responseCode, HttpServletRequest request, HttpServletResponse httpServletRequest) throws IOException; void addHandler(int responseCode, String handlerDescription); } 
+1
source share

For a page error, see this answer: How can I make a filter to determine if the user requested a page that was not found?

  • Make filter
  • Use HttpServletResponseWrapper and override sendError () and setStatus ()
  • Pass the wrapped answer through chain.doFilter (req, wrapper)
  • If you received sendError () in your wrapper, see if it is 404.
  • Take the appropriate answer.
0
source share

You should try to create your own SessionListener, and then add it to the servletContext of your WebApplicationInitializer ..

Something like that:

 import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent evt) { System.out.println("Session created"); evt.getSession().setMaxInactiveInterval(30 * 60); // That means 30 minutes. } public void sessionDestroyed(HttpSessionEvent evt) { System.out.println("Session destroyed"); } } 

And then in your WebContentInitializer add this line:

 servletContext.addListener(new SessionListener()); 
0
source share

All Articles