wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Question on HttpSession Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Question on HttpSession" Watch "Question on HttpSession" New topic
Author

Question on HttpSession

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following JSP code
What will it print for the very first request to this page as well as the web application that contains this page?

<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>

Given answer: It will print Hello and will set the count attribute in the session.

According to me, since the parameter to request.getSession() is false in both the if and else clause(s), a session will not be created at all.
Shouldn't it throw a NullPointerException?
David Bridgewater
author
Ranch Hand

Joined: Apr 29, 2004
Posts: 44
Without trying the code... I think you will get a session by default. Unless you explicitly set a page directive in the JSP to say session="false", then the very beginning of the generated _jspService() method will obtain a session object. So when you use request.getSession(false) later in the code, there will be a session object there.

I'll try the code out now!

HTH,

David.
Jingh Yi
Ranch Hand

Joined: Mar 23, 2005
Posts: 90
Kedar,

I don't quite get your code snippet. Does the count attribute get set in the session somewhere else before you access it from jsp page?

Thanks,
Jenny
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Hi,
The answer is correct it will print hello. This is because the implicit session object will be present and because of that it won't throw NullPointerException.

On the other hand if you include "<%@page session="false"%>" in your jsp then we are saying that not to make the implicit session object available and that's why in this case it will throw NullPointerException.

Hope this helps.


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
so in this code... if we don't include this "<%@page session="false"%>
will it put session automatically even the page is viewed v.first time ?

how does it work is it like it will create a newsession before the page code runs and
when we reach the line request.getSession(false) ..
it will return only previos session and don't create new session...

so here the implicit session created is used with code getAttribute("count"); and as there is no "count" element ...it returns null


so no runtime error... is my understanding is right ?

pls reply


Thanks and Regards, Amit Taneja
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
so in this code... if we don't include this "<%@page session="false"%>
will it put session automatically even the page is viewed v.first time ?


Yes

so here the implicit session created is used with code getAttribute("count"); and as there is no "count" element ...it returns null so no runtime error... is my understanding is right


Yes you are right. When the page is visited first time the implicit object will be made available and request.getSession(false) will return the session object.

Hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on HttpSession
 
Similar Threads
Confused about getSession
Session related Question
another mock exam question
question about session?
doubt is session