• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session Object

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please find the piece of code below:


and the Result.jsp is as follows :


The output from log is as follows


signifying that Session object is not created (since false in getSession())

but the Result.jsp is displayed as


Now my question is when Null is displayed in the log, where did SessionID come from and is getting displayed in Result.jsp. I mean session is not created as false is passed as a parameter for getSession() method.

I'm bit confused here with these sessions topic. Please some one show me the light
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a JSP, a session object is automatically defined and made available.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesus Angeles:
In a JSP, a session object is automatically defined and made available.



... if you make a reference to the implicit session object, which you do.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do this in the JSP too, which uses the session but NOT the implicit session object:

 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I could not understand the replies given for my question. Does this mean that the session object referred in Servlet is different from what being used in jsp?

One more update on my question.

When I start the server for the first time and run my servlet, I'm getting Null for the session object, but in Result.jsp session id and object is displayed as mentioned initially.

But when I run the servlet again without restarting the server i.e refreshing the page, this time Null is not displayed. This time object is displayed in both servelt and jsp.

Can anyone explain me the phenomenon here?
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe our statement above was not complete.

It is like this.

First of all, you always have 1 or 0 session object alive. The session object in jsp and servlet are the same instance.

In a servlet, you have to create your own session using the getSession(true). If one exist already, the getSession(true) returns the existing session (a session object which could have been created either in the servlet or jsp).

In the jsp, if there is no session object yet, it is created automatically for you.

Does this mean that the session object referred in Servlet is different from what being used in jsp?


There can only be 1 session object at most at one time. It is shared by all servlets and jsps during that entire conversation with the server. Of course, you can kill it (using invalidate method) then create a new one, but there is always 1 at a time.


But when I run the servlet again without restarting the server i.e refreshing the page, this time Null is not displayed. This time object is displayed in both servelt and jsp.

Can anyone explain me the phenomenon here?

Your conversation (session) with the server is still alive. Your session object created in the jsp previously is still there. The server is considering your old conversation and this new one, as from the same user or session. Try close the browser and open a new one.
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I should have written it this way:

In a JSP, a session object is automatically defined and made available. If there is already an existing session object (probably created somewhere else like another jsp or servlet), that existing session object is returned.
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic