I am using Spring MVC and have successfully setup a WebApplicationInitializer (using Tomcat's ServletContainerInitializer), without any web.xml file. Adding filters (like Spring Security) and servlets (like Dispatcher) is no problem, and they work fine. I can also set init-params if I need to do so.
What I can't figure out is how to set up some of the special tags that normally exist in the web.xml. For example, I would like to set up a custom 403 error page. Usually I would do this in web.xml with:
<error-page>
<error-code>403</error-code>
<location>/accessDenied.html</location>
</error-page>
But I can't figure out how to do this inside the WebApplicationInitializer (which has access to the ServletContext).
I have the same problem with session-timeout and welcome-files. I've been searching for about two days, but still haven't seen this done programmatically. Again the goal is to completely remove the web.xml file and use the initializer class instead.
Any ideas?
You can use scheduler to forcefully destroy session after 30 minute if you store session creation time. You can start a scheduler on application start up and it will check the sessions which are older than 30 minute will be invalidated.
Doesn't look like this is possible through WebApplicationInitializer, you will have to stick to web.xml for specifically this configuration along with some others listed with this question - Using Spring MVC 3.1+ WebApplicationInitializer to programmatically configure session-config and error-page
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With