Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can different Servlet classes share a single session object?  RSS feed

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My web application requires customers to register.
I have a public class Login extends HttpServlet, in which I create a session object right after the customer successfully logs in, like so:
HttpSession session = request.getSession(true);
Now, suppose after the customer has logged in, he is directed to my catalog page, where he finds something he likes. And he clicks "Order this item", then he is sent to the order page. When he clicks "Check out" on the order page, another class is invoked. It is public class MakeOrder extends HttpServlet.
Here are my questions:
1. can the MakeOrder class use the same session object created in the Login class?
2. If yes, how? (probably not new Login().session.getAttribute(blah blah); )
3. If not, then in the MakeOrder class, how can I track down the same customer who logged in just now?
If I also need to create a session object in MakeOrder class, will this session object be the same as the one created in the Login class?
Thanks.
Gene
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
onces the session is created you can use same session object to track the user session.
All you have to do is
In your other servlet use
HttpSession session = request.getSession(false);
//by adding above line. If user calling the page
//is in the session it will give the handle to
//session object. Else it will return null
if(session==null){
//this means user is not in session send him to login page to log again
}
else{
//Allow user to shop
}
-arun
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha, thanks a lot.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!