user533 user533 - 1 year ago 77
Java Question

how to retrieve session value from one servlet to other servlet

In one servlet i have the four variables. i want all those four variables to be retrieved to another servlet.

I used the code in servlet 1 as follows.

import javax.servlet.http.HttpSession;


In other servlet i tried to get the value by using the code..

String id = HttpSession.getAttribute("id").toString();

I think there is clear way to do the tracking of the session variables.

I have seen in net but all are confusing to me..

Please help me..

Answer Source

First you need to get the Session object from the request.

This is the HTTPServletRequest object sent to the servlet (you will have access to this in the doGet or doPost method).

to set:

ses = request.getSession(true);

to retrieve:


getSession(true) means create session if one does not exist. getSession(false) is equal to getSession. Finally if you wish to remove the attribute from the session from that point you can use


I hope this makes sense to you if you need more look at Java Set, Get and Remove Session Attributes.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download