• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

question about session?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem

Consider the following JSP code (See exhibit).
What will it print for the very first request to this page as well as the web application that contains this page?

Code

<html><body>
<%
Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{
out.println(count);
}
else request.getSession(false).setAttribute("count", new Integer(1));
%>
Hello!
</body></html>

(Select 1 correct option.)

A. It will print Hello!
B. It will print Hello and will set the count attribute in the session.
C. It will throw a NullPointerException at request time.
D. It will not compile.

I will choose C, but the answer is B.
Why?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Romy,

In JSP, session tracking is true by default (<%@ page session=�true� %> . So when you load this JSP page, request.getSession(false) will return an existing session. Now, since the count attribute is not set in the session scope, getAttribute(�count�) will return null which means else clause is executed.

Hope this helps.
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer would be true for a servlet but JSP's operate a little differently. I will post a sample code which shows a _jspService method. There you can see that we already have a session object.


Hope this makes it clear!
 
Rancher
Posts: 3528
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Romy Huang:
Problem

Consider the following JSP code (See exhibit).
What will it print for the very first request to this page as well as the web application that contains this page?

Code

<html><body>
<%
Integer count = (Integer) request.getSession(false).getAttribute("count");
if(count != null )
{
out.println(count);
}
else request.getSession(false).setAttribute("count", new Integer(1));
%>
Hello!
</body></html>

(Select 1 correct option.)

A. It will print Hello!
B. It will print Hello and will set the count attribute in the session.
C. It will throw a NullPointerException at request time.
D. It will not compile.

I will choose C, but the answer is B.
Why?



Hi Romy !

The correct answer is "B".

JSP sessions are configured via 'page' directive and 'session' attribute.
By default sessions are always *enabled*. This is equal if you would have :



in your JSP.

So, when you call 'getAttribute("count")' the session already exists - it was *implicitly* created by container for you. No NullPointerException thrown.

If you put the following code in your JSP :



then you will get a NullPointerException in server's log file and 500 error in browser as you expected.

regards,
MZ
 
Romy Huang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!