• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException Question from Head first Servlets

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roel De Nijs
Sheriff
Posts: 9795
101
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()


  •  
    Mohit G Gupta
    Ranch Hand
    Posts: 634
    Chrome Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 256
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 9795
    101
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 144
    Fedora Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Roel De Nijs
    Sheriff
    Posts: 9795
    101
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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"));
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic