• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IllegalStateException Question from Head first Servlets

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Eclipse IDE Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 256
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 144
    Oracle Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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"));

     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic