Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session related Question

 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a meaningful subject for your threads, and avoid abbreviations like "Q". Read this about how to ask questions at the ranch.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As explained by Anand, the out should be Hello! for the first request
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will find this useful..click here It is related to the same question

Thanks & Regards,
Sudhakar Karnati
 
deepa raj
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for all...

I got it.

Session creation is different in JSP and Servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic