Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep session after browser restart java [duplicate]

I'm working on a small project with java servlets/jstl

I've created a login with a session and I want the browser to keep that session even after a browserrestart.

I've written this code:

HttpSession session=request.getSession();
session.setMaxInactiveInterval(604800);
session.setAttribute("loggedOnUser", true);

I've set the session timeout to a week. But whenever I close the browser and reopen it I need to login again. When I look at the cookies of my browser, the cookie that contains the sessionId still expires when the browser closes. I thought "setMaxInactiveInterval" would change that to one week. Does anyone know what the problem is?

like image 599
Bosiwow Avatar asked Dec 28 '14 09:12

Bosiwow


2 Answers

I suggest setting the max-age of that cookie:

HttpSession session = request.getSession();
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
like image 74
Anirudha Avatar answered Nov 11 '22 18:11

Anirudha


when browser restarts some browser deletes cookies and that is why when after restart when you make new request server doesn't see cookie in request and treats it as a new session

like image 1
jmj Avatar answered Nov 11 '22 16:11

jmj