• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Session related Question

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...!!
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for all...

I got it.

Session creation is different in JSP and Servlet.
 
No holds barred. And no bars holed. Except this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic