• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Piotr Nowicki
Ranch Hand
Posts: 611
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
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE 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: 611
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic