简体   繁体   中英

Trying to retrieve arraylist from session in servlet

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?

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() ?

尝试强制转换为List<UserApplication>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM