protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name"); // get param
List<String> list = new ArrayList<String>(); // create list
HttpSession session = request.getSession(); // create a session handler object
// if this is new session , add the param to the list, then set the list as session atr
if(session.isNew()) {
System.out.println("in new session");
// this is a new session , add the param to the new list, then add list to session atr
list.add(name);
session.setAttribute("list", list);
}else{
System.out.println("in old session");
// THIS SESSION ALREADY EXISTS (THERE IS DATA IN LIST THAT WE NEED, THAT DATA IS STORED IN SESSION ATR)
// get the session atr , then store the content of a atr list, to this new list
list = (List<String>)session.getAttribute("list");
// add the new item to the list
list.add(name);
// set the new session atr, now with this newly added item
session.setAttribute("list", list);
}
Pretty much my comments say it all. I redirect from jsp page from where a from is submitted, get the name , create a list and session handler.
Point of this program is saving user input in a list. Obviously, I need sessions so I can make difference between users and have different lists for different users. I get null pointer exception in else statement where I try to retrieve an already existing list so I can add more items in it. What am I missing ? Thanks