Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

SWCD Study Companion - Ch 8, Q.10 - wrong answer?

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Charles Lyons book, you can read:

Chapter 8, Question 10 (2nd Edition):

Study the exhibit. What is the result of compiling, deploying and issuing a request to this servlet?



A. The session is invalidate, the userid attribute removed from session-scope and the request returns.
B. A NullPointerException is thron on line 8.
C. An exception is thrown on line 9.
D. An exception is thrown on line 10.
E. A compilation error occurs.



The answer which is given in the book is: C.

Well, for me it looks like it should be E, as there is no casting to String when using HttpSession#getAttribute(-) method which returns an Object.

EDIT: line numbers appended
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct... Excluding it, the exception will be thrown due to the usage of s.getAttribute("userid") after invalidating the session
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ram!

Yes, I agree, if there would be a cast to String, or the String userid would be changed to Object userid, than the IllegalStateException would be thrown.

Cheers!
 
I want my playground back. Here, I'll give you this tiny ad for it:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic