• 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

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

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True!
 
Bhumika Thakkar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it would return "null".

Cheers,
John
reply
    Bookmark Topic Watch Topic
  • New Topic