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

Session in a servlet

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a session? How do you implement a session in servlets?
 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A session is shared state information between the
server and the client. You do not have to implement
it in servlets; the container vendor has to implement
it for you.
There is a servlet trail in the Java tutorial
that includes some info on saving state. Check
http://java.sun.com/docs/books/tutorial/servlets/TOC.html
Also, the current Servlet 2.3 spec discusses sessions,
and what the servlet container must do to support
sessions for the application developer. That spec
is available at
http://java.sun.com/products/servlet/download.html
HTH,
Joe
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically sessions are object that allows you to mantain the state of a user between servlets calls.
[CODE]
// the next code excerpt set an attribute in a
// session if there's no userName in the session.
.
.
.
public void doGet( HttpServletRequest request,
HttpServletResponse response )
{
.
.
.
HttpSession session = request.getSession();
if( session.getAttribute( "userName" ) == null ){
session.setAttribute( "userName","guest");
}
.
.
.
.
}

see the API for more information:http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a session is a way for us programmers to keep a tab on user sessions. that means that because the http protocol doesnt allow us to know if it is the same user accesing several pages in our web site, we have somethn called a session.
in order to explain this better u need to understand several concepts:
first , every time a user access a page he makes a REQUEST to that page.
we can handle his REQUESTS per each time he access a page.
however we wish to know that even if the user access several pages all in the same SESSION (as in transaction) we have to keep some kind of tab to know thats it the same user in the same transaction.
in order to do that in servlets we can sort of put a cookie in his side letting us know in every page that its the same user(by identifying his cookie). this cookie is made easy to put as in the form of a session.
in servlet we can do:
HttpSession user=request.getSession();
this get the current session for us or creates one if there it is the first page visited.
thats it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic