Brief description  about Online courses   join in Online courses
View Smita Mishra 's Profile

set and get the variable through 2 requests in session

How to set a variable in session in first request and get the same variable in second request?
Asked by Smita Mishra | Oct 1, 2014 |  Reply now
Replies (1)
View arun kumar das 's Profile
Suppose you have a jsp page with mulitple link buttons. When you click on any button a servlet is called with respective parameters.

For example 1 is passed if button1 is clicked, 2 if button2 is clicked,etc.

Servlet gets an arraylist from session variable which is already created earlier and depending on requested parameter gets the data from arraylist, processes it and sends response to another jsp say jsp2.

jsp2 also has similar link buttons and should do the same task. When the same Servlet is called from jsp2 or even if the page is refreshed, the session variable is null this time.

In my web.xml file under the session-config tag I have set the timeout to -1 so that session never expires.


In the servlet I get the session variable as- HttpSession session = request.getSession(false);

ArrayList<String> list = new ArrayList<String>();
list = (ArrayList<String>)session.getAttribute("mylist");

When the servlet is called second time or even if the page is refreshed the list is null. I put few lines to check if session is valid-and it prints the second time.

System.out.println("Session is Expired !!");

and the session.getAttribute("mylist") returns NullPointerException.
Oct 29, 2014