*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Session creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Session creation" Watch "Session creation" New topic
Author

Session creation

Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
When is a session created ?

What would be the result of request.getSession(false);
if this is the first request to this page and to the web application too.

Would a session be already created ? else it should return null ?

Please elaborate.

Thanks in advance


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
A session is created when you create it.
HttpSession sess = request.getSession().
This is the line that creates the session.
With regards,
Padma priya N.G.


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
request.getSession(false) will return null.
With regards,
Padma priya N.G.
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Thanks for your help. However, my question is in context to the following question from a mock exam

8.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?
<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.
ANS : B
If the session returns null then the answer should be C here. Any explainations ?
Sahid Khan
Ranch Hand

Joined: Jun 27, 2007
Posts: 41
request.getSession(false) will return a session if there is any session associated with the requested, if not it will return null.

request.getSession() will create a session if there is no session associated with the request. request.getSession() is same as request.getSessin(true)
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218

Swati said:
Thanks for your help. However, my question is in context to the following question from a mock exam

8.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?
<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.
ANS : B
If the session returns null then the answer should be C here. Any explainations ?



Hi swati, in a JSP you have the implicit session variable available with you. So by the time you execute the statement "request.getSession(false).getAttribute("count");" you already have the session created. Note that there is no page directive
<%@ page session="false" %> as well. So session is available in JSP by default. If you put the above directive in the page then your answer is correct.

To get a clear idea see the java code generated for this jsp by the container.

Hope its clear for you now
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Thank you Amol, thats the answer I was looking for .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Session creation
 
Similar Threads
managed-property and query string
Correct way to use getSession() in actions?
Persisting shopping cart values.
Session creates or not if we write request.getSeeion(false) for the first time
feel confused about httpsession