0
votes

I'm using HttpSession to store ArrayList userList as session attribute from original Servlet before passing it to JSP. It is then called in the next JSP and then called to another Servlet from that JSP.

Servlet 1 -> JSP1 -> JSP2 -> Servlet 2

In Servlet 1, I've set it to session :

if (!userList.isEmpty()) {
    session.setAttribute("userList", userList); 
}    

I iterate it in JSP 1 and JSP 2 and call it again in Servlet 2. I need the ArrayList to be used as a parameter in another method in servlet 2.

EditStudentForm edt = (EditStudentForm)form;
List<UserApplication> studtList = new ArrayList<UserApplication>();
if ((session.getAttribute("userList")) instanceof List){
    studtList = (ArrayList<UserApplication>)request.getSession().getAttribute("userList");
}
try {
    uaDAO.editUser(edt,studtList);
    action_forward = EDITSUCCESS;
}

It looks like the casting is not really working because the size of the ArrayList is 1 (I'm expecting a size of at least 30)

What am I doing wrong?

2
What is the error you are getting? - Amit
umm.. a) is this all within a single request b) if it is, why aren't you saving it as a request attribute c) if not, what is your mechanism from going to servlet from a jsp d) even if it is, why would you go to servlet from a jsp? - eis
If it's in the session, it's in the session. Are you sure you're accessing the data you think you are? Also, don't cast to ArrayList if you're checking for a List--cast to List. - Dave Newton
@eis Because servlets are where all the back-end work occurs, not in JSPs, which are the presentation layer. Although this sounds more like Struts than servlets. - Dave Newton
@DaveNewton servlets are typically front controllers, not "where all the back-end work occurs". but either or, I'm curious why would you go back to a servlet from a jsp in a request chain. - eis

2 Answers

0
votes

Casting is working fine and is unrelated to the size of the list. Casting just tells you that what you've stored in userList is really an ArrayList. If the size is different, then it seems to imply there is a bug in your logic somewhere else. Do you change the list referred to by the variable userList after calling session.setAttribute()?

0
votes

Try casting to List<UserApplication>.