Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 599
BobRob Avatar asked Jul 24 '12 15:07

BobRob


People also ask

How to solve session timeout problem in Java?

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.


2 Answers

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

like image 146
Biju Kunjummen Avatar answered Sep 28 '22 07:09

Biju Kunjummen


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);
like image 21
user3184922 Avatar answered Sep 28 '22 07:09

user3184922