Hi, If I set session.setMaxInactiveInterval(0), can I use session.getAttribute("key") and will I get the value that was set just before calling setMaxInactiveInterval(0). According to HFSJ book i should get IllegalStateException, but when i tested and executed i am getting the value. I guess that is the time between two requests. Only next request will not be able to get that value. In the same request, i should be able to get that value without any exception. Am I right? Even I am not able to see that entry in errata. Appreciate your response in earlier.
Rizwan
SCJA, SCJP, SCWCD, SCBCD, SCDJWS.
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
posted
0
According to the errata, that piece of code should be changed as below. If you do, you would get the exception because of the isNew method call. Yes, you are correct, if you leave it the old way, you can probably still access the session attribute on the same request.
session.setMaxInactiveInterval(0); if(session.isNew()){ out.println("This is a new session"); }else{ out.println("Welcome back!"); } out.println("Foo: " + session.getAttribute("foo")); [ October 30, 2005: Message edited by: Bosun Bello ]
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sub swamy
Ranch Hand
Joined: Oct 02, 2002
Posts: 121
posted
0
Tested the below code. Got IllegalStateException on both occassions after invalidating session - when testing for session.isNew() and while trying to get attribute from session.
And as has been specified in HFSJ was able to get a reference to ServletContext object(which is the only method that can be called in an invalidated session) after invalidating session.
Hi Subramanian, session.setmaxinactiveinterval(0) doesnt mean that session is invalidated. session.invalidate() causes the session to invalidate immediately. when setmaxinactiveinterval, i guess it affects only on next request. tested on was 4.0 server.
Balasubramani Dharmalingam
Ranch Hand
Joined: Dec 06, 2004
Posts: 116
posted
0
hai Rizwan , when i tried the following code in tomcat 5.0.28 , i am getting IllegalStateException .
tomcat 4.0 is for servlet 2.3 spec . actually setMaxInactiveInterval( 0 ) will immediately invalidate the session .
Balasubramani Dharmalingam wrote:hai Rizwan ,
when i tried the following code in tomcat 5.0.28 , i am getting IllegalStateException .
tomcat 4.0 is for servlet 2.3 spec . actually setMaxInactiveInterval( 0 ) will immediately invalidate the session .
is true. but take a look at the javadocs on netbeans..
public void setMaxInactiveInterval(int interval)
Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
An interval value of zero or less indicates that the session should never timeout.
Parameters:
interval - An integer specifying the number of seconds
Be careful here! There has been a change between EE5 and EE6:
EE5 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
A negative time indicates the session should never timeout. "
EE6 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
An interval value of zero or less indicates that the session should never timeout."
In EE5 zero means invalidate after 0 seconds: meaning immediately, now in EE6 it will not timeout at all...
Regards,
Frits
Cristian Daniel Ortiz Cuellar
Ranch Hand
Joined: Feb 02, 2011
Posts: 81
posted
0
Frits Walraven wrote:Hi there,
Be careful here! There has been a change between EE5 and EE6:
EE5 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
A negative time indicates the session should never timeout. "
EE6 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
An interval value of zero or less indicates that the session should never timeout."
In EE5 zero means invalidate after 0 seconds: meaning immediately, now in EE6 it will not timeout at all...
Regards,
Frits
wow. Well Done Frits Walraven.. now it's very clear..
I tried the code on tomcat 6 as well as tomcat 7
HttpSession t=req.getSession();
t.setAttribute("foo", "foo1");
t.setMaxInactiveInterval(0);
resp.getWriter().println("d"+t.getAttribute("foo"));
Both gave a IllegalstateException.Please help out fritz.
Cristian Daniel Ortiz Cuellar
Ranch Hand
Joined: Feb 02, 2011
Posts: 81
posted
0
Ankur Gargg wrote:Hi
I tried the code on tomcat 6 as well as tomcat 7
HttpSession t=req.getSession();
t.setAttribute("foo", "foo1");
t.setMaxInactiveInterval(0);
resp.getWriter().println("d"+t.getAttribute("foo"));
Both gave a IllegalstateException.Please help out fritz.
i think tomcat 6 or tomcat 7 doesnt mean anything about it..
I am using apache tomcat 7 which means my code is JEE6 compliant.
also I compiled them using the servlet-api.jar of tomact 7.
may be you can try it on your machine..
Be careful here! There has been a change between EE5 and EE6:
EE5 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
A negative time indicates the session should never timeout. "
EE6 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
An interval value of zero or less indicates that the session should never timeout."
In EE5 zero means invalidate after 0 seconds: meaning immediately, now in EE6 it will not timeout at all...
Frits can you share the ink from where you got this inforamtion.As I am running same program on both JEE5 container as well as JEE6 container but getting the same big fat IllegalstateException.
Cristian Daniel Ortiz Cuellar
Ranch Hand
Joined: Feb 02, 2011
Posts: 81
posted
0
Ankur Gargg wrote:
Frits Walraven wrote:Hi there,
Be careful here! There has been a change between EE5 and EE6:
EE5 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
A negative time indicates the session should never timeout. "
EE6 "Specifies the time, in seconds, between client requests before the servlet container will invalidate this session.
An interval value of zero or less indicates that the session should never timeout."
In EE5 zero means invalidate after 0 seconds: meaning immediately, now in EE6 it will not timeout at all...
Frits can you share the ink from where you got this inforamtion.As I am running same program on both JEE5 container as well as JEE6 container but getting the same big fat IllegalstateException.
i am giving a try ready now..
the results..
the output...
in java EE 5 and java EE 6. setMaxInactiveInterval(0); will invalidate inmediataly the session..
in java EE 5 and java EE 6. setMaxInactiveInterval(0); will invalidate inmediataly the session..
based on my machine.. best regards..
This means that Tomcat has not implemented the EE6 to the full extend as the Servlet 3.0 clearly states:
7.5 Session Timeouts The default time out period for sessions is defined by the servlet container and can be obtained via the getMaxInactiveInterval method of the HttpSession interface.
This time out can be changed by the Developer using the setMaxInactiveInterval method of the HttpSession interface. The time out periods used by these methods
are defined in seconds. By definition, if the time out period for a session is set to 0 or lesser value, the session will never expire. The session invalidation will not take
effect until all servlets using that session have exited the service method. Once the session invalidation is initiated, a new request must not be able to see that session.
Be sure that you know what is in the specifications and not what Tomcat does when you are in the exam room
Regards,
Frits
Milesh Bhana
Greenhorn
Joined: Feb 18, 2010
Posts: 5
posted
0
The Enthuware mock exam also states that setMaxInterval(0) will invalidate the session.
Can i assume this is an errata on their side too.
(Always assumed that theirs was pretty close to the real thing)
If you have the EE6 version of their software please give them feedback about it and they will fix it for sure.
Regards,
Frits
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
posted
0
I just checked Servlet 3.0 Specification (Final) and it states:
Section 7.5 Session Timeouts
The default time out period for sessions is defined by the servlet container and can
be obtained via the getMaxInactiveInterval method of the HttpSession interface.
This time out can be changed by the Developer using the setMaxInactiveInterval
method of the HttpSession interface. The time out periods used by these methods
are defined in seconds. By definition, if the time out period for a session is set to -1,
the session will never expire.
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2912
posted
0
Update:
Frits is right. The statement was updated to " By definition, if the time out period for a session is set to 0 or lesser value, the session will never expire." in revision A of 3.0 specification ( Feb 2011) and the same statement exists in Servlet 3.1 Early Draft ( June 2012 ) as well. So it seems they have made up their mind now. We will update the question accordingly.