• Post Reply Bookmark Topic Watch Topic
  • New Topic

Session handling in servlets  RSS feed

 
shawn kennedy
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to know how to maintain a session in servlet.Do i have to use
HttpSession eg=request.getSession();
eg.setAttribute(name,value);
if yes how do i get the session info on every page (do i have to use eg.getAttribute(name)on every page will tht work)so i know a user is a part of present session and is not accessin pages out of order.
Or do i have to use cookies.If cookies how do i use them in all the pages of an application say it is defined on one page.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66149
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cookies will be used to maintain the session behind the scenes, but you shouldn't need to be overly concerned about that. As you surmised, your primary contact with the session will be obtaining a reference to it, and using get/set attribute on it.
hth,
bear
 
bharat nagpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet Session can be maintained in one of the follwing ways
1) URL rewriting
2) hidden fields
3) cookies
4) Session Object
If you use session object(Method 4), This is how it goes.
For the very first request from client, the Server generates
a session object for the client and
a unique session id for that client, sends session id back to the Client.
(On the server assume that this is stored in some Hashtable, with uniques session id as key and session object as value)
The session id that server sends to the client is stored in some temp location on the client machine.
and with the next request, session id is sent back to the server, so that Server can know which client has sent the request.
In this way Statelessness nature of HTTP protocol is handled.
What you need to do is to store value in the session object and you can get that value back from the session object anywhere in the same application, using setAttribute() and getAttribute() methods.
Disadvantage of using Session object is that it places a lot of data on the server.Almost all new servers are capable of using the hard disk memory so they can handle this thing. but programmer should ensure that he would be using session object only for important and not to large data.
If you want to use methods 1,2 or 3, you will have to write your own stuff to handle them , but they won't use any memory in the server.Each method has it's own advantages and disadvantages.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bhart nagpal:
Servlet Session can be maintained in one of the follwing ways...

You mean state.
All of your mechanisms are ways of maintaining state across HTTP, which is stateless by nature.
A Session refers to data storred on the server and it is associated with a user using a session cookie, which is the same as regular Cookie, but is linked to a browser instance* so that the session dies on the client side if the browser is closed.
(*) it is possible to have multiple browser instances share a session, and it is lost once all the browser windows have closed, and it is possible to have multiple browser windows with different sessions.
if yes how do i get the session info on every page
The server maintains the session for you. Ads long as there is a session, asking for it will give you the same session back. (check the documentation for getSession in the javadocs)
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!