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.
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 ]
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..!!
Joined: Apr 16, 2004
session will have been created by the time of second request and hence it is available even we are passing false to getSession method.