File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setMaxInactiveInterval to 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "setMaxInactiveInterval to 0" Watch "setMaxInactiveInterval to 0" New topic
Author

setMaxInactiveInterval to 0

Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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: 1510
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
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.

Env: Tomcat 5.0.30/Win 2000
Rizwan Mohammad
Ranch Hand

Joined: Sep 02, 2005
Posts: 445
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
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 SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
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

why is this happens. is a kind of strange...



[Thumbnail for Session.png]

Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

Hi

I tried this code

HttpSession t=request.getSession();
t.setAttribute("foo", "foo1");
t.setMaxInactiveInterval(0);
4)response.getWriter().println("d"+t.getAttribute("foo"));



It is working as we all expected.It throws a big fat "IllegalStateException" on line 4.
I am not able to fathom what the confusion is.



Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
Ankur Gargg wrote:Hi

I tried this code

HttpSession t=request.getSession();
t.setAttribute("foo", "foo1");
t.setMaxInactiveInterval(0);
4)response.getWriter().println("d"+t.getAttribute("foo"));



It is working as we all expected.It throws a big fat "IllegalStateException" on line 4.
I am not able to fathom what the confusion is.


t.setMaxInactiveInterval(0); is equals to invalidate.. just the JavaDoc is wrong... the javadoc works very good ON DD. not in CODE..
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

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
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
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..

thanks bro.
Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

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.

Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
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..

you can use tomcat 5,6,7 with java ee 5.

make sure you are using java 6 (the underlying web container doesnt matter.).. to test it....
Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

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..
Ankur Gargg
Ranch Hand

Joined: Sep 11, 2011
Posts: 55

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.
Cristian Daniel Ortiz Cuellar
Ranch Hand

Joined: Feb 02, 2011
Posts: 81
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..

based on my machine.. best regards..
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

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
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)


OCJP6 88%, OCWCD6 92%
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

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: 3336
    
    8
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.



So the answer given in our s/w is correct.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3336
    
    8
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.

thank you,
Paul.
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 319

I was also trying the example in the HeadFirst servlet jsp book.
And calling setMaxInactiveInterval(0) does not invalidate the session immediately.

Thanks to Frits ,


http://www.mahtabalam.net , Oracle Java Programmer , Oracle SQL Expert , Oracle Java Web Component Developer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setMaxInactiveInterval to 0