Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@page session=false. request.getSession() throws error?

 
Bhumika Thakkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the outcome of a user accessing the following uncompiled JSP for the first time in a fresh browser session?
Note that the line numbers are for reference, as if the code appeared in a text editor.

[CODE:XML NUM]
<HTML><HEAD></HEAD><BODY>
<P>Session ID: <%-- <%= session.getId() %> --%></P>
<%@ page session = "false" %>
<%
HttpSession session = request.getSession(false);
boolean b = (session == null) ? true : false;
%>
<%= "There is no session: " + b %>
</BODY></HTML>
[/CODE:XML]

Possible Answer(s)
1) A runtime error will occur, caused by a NullPointerException
2) A compilation error will occur because of the misplaced page directive at line 12, which should come before any JSP scriptlets or expressions.
3) A compilation error will occur because the variable "session" will be declared twice - once as an implicit JSP variable, and again at line 14.
4) Ouput will be obtained similar to the following:
Session ID:
There is no session: false
5) Ouput will be obtained similar to the following:
Session ID:
There is no session: true
6) Ouput will be obtained similar to the following:
Session ID: 00JPQXYABCD1234888894RTV
There is no session: false

Correct Answers
Ouput will be obtained similar to the following:
Session ID:
There is no session: true
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, thsi one's from javablackbelt. the ans is right.
 
Bhumika Thakkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
don't u think session.getId() should throw a NullPointerException.
 
Kejal Shah
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its inside a JSP comment which won't be there in the servlet. Wat say?
 
Bhumika Thakkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True!
 
Bhumika Thakkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@page session=false means the page wont' get the implicit session object. does it mean that i CANNOT take part in session?
if so then request.getSession(true) should throw some error right?

Bhumika
 
John Mallavalli
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it would return "null".

Cheers,
John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic