Maria
Maria

Reputation: 169

How can I pass session from one servlet to another

I have a servlet ,it creates the session and I need to pass that session to another servlet, Is that possible ?

Upvotes: 1

Views: 805

Answers (1)

M4ver1k
M4ver1k

Reputation: 1565

Yes you can.

Sessions are not specific to a servlet it is managed by your servlet container. So even if you forward request from one servlet to another and use request.getSession()the session will persist, provided you dont call the invalidate() or session timeout didn't occurs and both the servlets belong to same web application.

To add on : When the first request comes from a user he is assigned a session and all further request\response will have in same session unless the session expires either due to session timeout or invalidate() method was called. The container manages session with two methods:

  1. URL Rewriting.
  2. Cookies.

Upvotes: 1

Related Questions