Reputation: 169
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
Upvotes: 3
Views: 12243
Reputation: 1108577
That's indeed not the right approach of maintaining a session scoped object. You're relying on HttpSession#isNew()
which would only return true
when the HTTP session is new, not when the session scoped object is absent. If the session has already (implicitly) been created before the servlet is invoked, then isNew()
would return false
. For example, when you've opened a JSP page without <%@page session="false" %>
beforehand in the same session.
You should instead be checking if the session scoped object of interest is present or not.
So, instead of:
HttpSession session = request.getSession();
List<String> list = new ArrayList<String>();
if (session.isNew()) {
list.add(name);
session.setAttribute("list", list);
} else {
list = (List<String>) session.getAttribute("list");
list.add(name);
session.setAttribute("list", list);
}
You should be doing like:
HttpSession session = request.getSession();
List<String> list = (List<String>) session.getAttribute("list");
if (list == null) {
list = new ArrayList<String>();
session.setAttribute("list", list);
}
list.add(name);
Do note that you do not need to put that list back in the session. The session does not hold a copy of the List
object or so, as you'd expect in procedural languages like PHP. Java is an object oriented language. The session holds a copy of the reference to the List
object. All changes to mutable objects like List
are reflected back in all references.
To learn how the HTTP session works under the covers, head to How do servlets work? Instantiation, sessions, shared variables and multithreading.
Upvotes: 5