Bookmark Topic Watch Topic
  • New Topic

sessions

 
Ravissant Markenday
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi everyone !!

i am doing sessions and man am i confused! now what i want to know is suppose i want servlet A to call servlet B. Then in which servlet (A or B) do i create the HttpSession object??

Thanks in advance,
Ravissant
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
You don't create the session object.
The container does.

Also servlets don't "call" one another.
They can forward context to one another via a requestDispatcher.
They can also use sendRedirect to cause a new request to another servlet to be generated.
They can also build HTML output with hyperlinks or HTTP forms that the user will click on to send a new request to another servlet.

In any of these cases request.getSession() will give you a reference to the session object, created by the server on your behalf.
You can then use the session's setAttribute(String, Object) method to bind objects to it.
From your other servlet, (or from this servlet in another request) you can retrieve a reference to that object with the session's getAttribute(String) method.

See:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Duplicate of:
http://www.coderanch.com/t/363778/Servlets/java/session-management

Ravissant Markenday,
Please don't cross-post or ask you question multiple times in the same forum.
Doing so wastes the time of those who would help you.
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!