8

I have a requirement to set the session timeout of 40 seconds. I know we keep normally to 20 minutes.

But my current application requirement is to keep the session timeout to 40 seconds. The web.xml is taking only integer value as 1 but it is not taking 0.6. Is there any way to write this? We are running our java web application on Apache tomcat server.

So how do I set session timeout in seconds in web.xml?

peterh
  • 11,875
  • 18
  • 85
  • 108
user3488632
  • 109
  • 1
  • 3
  • 7

5 Answers5

14

Using the deployment descriptor, you can only set the timeout in minutes:

<session-config>
    <session-timeout>1</session-timeout>
</session-config>


But using the HttpSession API you can set the session timeout in seconds for a servlet container:

HttpSession session = request.getSession();
session.setMaxInactiveInterval(40);

Suggested reading: Deployment Descriptor Elements

informatik01
  • 16,038
  • 10
  • 74
  • 104
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
3

well in web.xml file you can provide in minutes

<session-config>
    <session-timeout>Minutes</session-timeout>
</session-config>

but you programatically provide values in seconds

HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);
Sanjay Rabari
  • 2,091
  • 1
  • 17
  • 32
0
1) Timeout in the deployment descriptor (web.xml)

– Specified the timeout value in “minute” , enclose with “session-config” element.

Markup

<web-app ...>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
</web-app>

The above setting is apply for the entire web application, and session will be kill by container if client doesn’t make any request after 20 minutes.

2) Timeout with setMaxInactiveInterval()

– You can manually specified the timeout value in “second” for a particular session.

Java

HttpSession session = request.getSession();
session.setMaxInactiveInterval(20*60);

The above setting is only apply on session which call the “setMaxInactiveInterval()” method, and session will be kill by container if client doesn’t make any request after 20 minutes.

Mohit Singh
  • 5,977
  • 2
  • 24
  • 25
0

you can override the session timeout through "setMaxInactiveInterval()".

HttpSession session = request.getSession();
session.setMaxInactiveInterval(20000);

here it will take the time in milliseconds, means in next 20 seconds session will get expire.

Rahul Kumar
  • 145
  • 9
0

How do i set session timeout in seconds in web.xml? Solution: Try this const token = jwt.sign({ _id: user._id }, process.env.JWT_SECRET) // Your token here res.cookie('Cookie', token, { expire: new Date(Date.now()+ 9999)})

Sajid Khaki
  • 121
  • 1
  • 3