aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes  IllegalStateException  Question from Head first Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark " IllegalStateException  Question from Head first Servlets" Watch " IllegalStateException  Question from Head first Servlets" New topic
Author

IllegalStateException Question from Head first Servlets

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

head first servlets and jsp: pg-249 Topic:Session Management subtopic:be the container


IInd part


Result: a runtime exception (IllegalStateException) is thrown because you can’t call isNew() on the session AFTER the session becomes invalid. Setting the maximum inactive interval to 0 means the session times out and is invalidated immediately!

i think exception should be because of line source of exception(see comments) as session times out before the getAttribute command.
please correct me if i am wrong and do give explanation


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

I checked page 249 of the Head First book and I notice your code example is mixing up the 2 examples on this page (which makes it not compile at all):
  • example 1 uses session.invalidate() to invalidate the session and is followed by a call to getAttribute()
  • example 2 uses session.setMaxInactiveInterval(0) to invalidate the session and is followed by a call to isNew()



  • SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Mohit G Gupta
    Ranch Hand

    Joined: May 18, 2010
    Posts: 634

    I checked page 249 of the Head First book and I notice your code example is mixing up the 2 examples on this page (which makes it not compile at all):


    i think Roel ,you check it once again and i am not mixing the two questions
    I am asking for the second part of the "be the container.

    According to me,
    a runtime exception (IllegalStateException) is thrown because you can’t call session.getAttribute after
    session.setMaxInactiveInterval(0);
    but the explanation in book for the exception is different
    bhanu chowdary
    Ranch Hand

    Joined: Mar 09, 2010
    Posts: 256
    mohitkumar gupta wrote:
    According to me,
    a runtime exception (IllegalStateException) is thrown because you can’t call session.getAttribute after
    session.setMaxInactiveInterval(0);
    but the explanation in book for the exception is different


    According to the HttpSession spec the getAttribute("string") should throw an IllegalStateException if it is called on a session that is invalidated. So, even I think the exception is because of a call to getAttribute on the session.
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5216
        
      12

    mohitkumar gupta wrote:i think Roel ,you check it once again and i am not mixing the two questions
    I am asking for the second part of the "be the container.

    I don't know what kind of copy of the book you have, but in my copy there is no call at all to session.getAttribute() after the session is invalidated via session.setMaxInactiveInterval(0);

    The example looks like:

    If the last line is removed (or commented), because that generates a compiler error (variable 'foo' is not declared), the answer in the book is correct. The call to isNew() will generate a runtime exception.
    Mike Zal
    Ranch Hand

    Joined: May 04, 2011
    Posts: 144

    You should read the books errata page


    Page 249
    2nd Answer's Result (Last paragraph) of Chapter 6

    IllegalStateException is thrown before it reach the isNew() method on session. Since session.setMaxInactiveInterval(0) will cause the session invalidated immediately, the next line of code session.getAttribute("foo") method will throw IllegalStateException before it reach session.isNew() method.

    Note from the Author or Editor:
    VALID

    Remove the line of code that reads "String foo = (String) session.getAttribute("foo");
    On pg 249: also remove that line


    OCJP6, OCWCD5
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5216
        
      12

    I'm quiet convinced that erratum is just wrong for the printed version which I have (2nd edition of the book). On the errata-page it's indicated as an erratum for the PDF-version.

    In the printed version there is no line "String foo = (String) session.getAttribute("foo");", so it would be hard to remove it. And because that line isn't there, the foo-variable in the last line will result in a compiler error.

    The correct erratum would be something like (or just delete the last line of code):
    out.println("Foo: " + foo);
    should read:
    out.println("Foo: " + session.getAttribute("foo"));
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: IllegalStateException Question from Head first Servlets