File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Session related Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Session related Question" Watch "Session related Question" New topic
Author

Session related Question

deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
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?




Answer please.
[ August 06, 2008: Message edited by: deepa raj ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Please use a meaningful subject for your threads, and avoid abbreviations like "Q". Read this about how to ask questions at the ranch.


[My Blog]
All roads lead to JavaRanch
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

I was really horrified when I saw the subject line "Q" as wasn't sure what I was going to see...

Anyway, the output can't be said for certain. Ideally, for the first request the value of "count" variable will be null(unless explicitly set) and thus the output of the scripplet will be nothing, except it will bind the value 1 to the "count" variable.

Well, the gotcha is that you never know if a ClassCastException may head your way while casting the value as Integer.

Hope that helps...!!
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
As explained by Anand, the out should be Hello! for the first request


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
for the very first request , session will not be created.

so req.getSession(false) will return null.

I guess it should throw NullPointerException.is it so?

Please corretc me if i am wrong.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by deepa raj:
for the very first request , session will not be created.

so req.getSession(false) will return null.

I guess it should throw NullPointerException.is it so?

Please corretc me if i am wrong.



You got to really understand the various scopes available.
The biggest of them is context(application), then comes session, and then the request scope. (although there is another scope as page scope, but we are not disussion scoping here)

Thus, to have a request object you got to have the two upper level objects available.
So, when you say request.get(.....) then session scope is by default available.

As a very raw example consider that when a user(consider it the very first user) opens a browser and hits the url for an application in a web container the context is initialized. Then the session is created for that user which you may consider as the time his browser is active, unless session expiry is set. Then whatever he queries with the container is all request..

But, again a gotcha is there might be no active session at the time you do the query. So, in that case what you say is inherently true..

Hope that helps.
[ August 06, 2008: Message edited by: Anubhav Anand ]
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Just thought to add up this minute detail:

getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
getSession(boolean create)
Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.


Well, by default the session attribute in the page directive is true.So every jsp page contains session object, unless you explicitly say:


Hope that helps..!!
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
session will have been created by the time of second request and hence it is available even we are passing false to getSession method.
sudhakar karnati
Ranch Hand

Joined: May 03, 2007
Posts: 93
I think you will find this useful..click here It is related to the same question

Thanks & Regards,
Sudhakar Karnati
deepa raj
Ranch Hand

Joined: Jul 25, 2008
Posts: 124
Thank for all...

I got it.

Session creation is different in JSP and Servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session related Question