This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

IllegalStateException Question from Head first Servlets

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome 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):
  • 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
    • 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
    • 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
    • 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
    • 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
    • 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"));

     
    They weren't very bright, but they were very, very big. Ad contrast:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic