I am new to JEE and this is what confuses me. According to HttpSession.html#setMaxInactiveInterval(int interval) documentation
An
interval
value ofzero
or less indicates that the session should never timeout.
but according to my text book (which already is few years old - so I expect it not to be always right) using zero as argument should cause session to timeout immediately.
This code
public class Test extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("foo", 42);
session.setMaxInactiveInterval(0);
out.println(session.getAttribute("foo"));//problem here
}
}
used on Glassfish 4.0 seems to confirm theory from textbook instead of newer official documentation because it returns HTTP Status 500 - Internal Server Error with error message
java.lang.IllegalStateException: getAttribute: Session already invalidated
What is going on here? Is this Glassfish 4.0 bug or documentation is wrong? Or maybe there is third option?
PS. This code works as it should with negative values (session is not invalidated) and I am using -1
instead of 0
in my code. I am just interested what is wrong with 0
.
-1
. – Johnathanjohnathon-1
works fine and since it seems safer I am using it instead of0
. I am just curious what is wrong with zero. – Croakersession-config
session-timeout
value to 0 in web.xml? – Johnathanjohnathonsession.setMaxInactiveInterval(0);
from code and use<session-config><session-timeout>0</session-timeout></session-config>
inweb.xml
it seems to be working fine (session is not destroyed). What is interestingsession.getMaxInactiveInterval()
returns-60
(in seconds) so it seems that value ofsession-timeout
is converted to-1
(in minutes) – CroakersetMaxInactiveInterval
is in seconds. – Johnathanjohnathon