Problem with arrayList e session jsp

I have a problem with my program.

I have a servlet; in this servlet save session attribute

ArrayList<Integer> list = new ArrayList<Integer>;
list.add(1);
request.getsession().setAttribute("list",list);

The attribute is now a String, not an ArrayList. In fact, when I try to do:

request.getsession().getAttribute(list)

is a string, not an array.

I need an array.

thank

+3
source share
3 answers

You need to enable it when you get the attribute from the session as follows:

 ArrayList<Integer> list = (ArrayList<Integer>)request.getsession().getAttribute("list");

And the attributes in the session are stored on the map, so the key you used is a string, and you need to use the string to retrieve the value.

+5
source

session.getAttribute(..) returns Object

You will need to do it like

List<Integer> list = (List<Integer>)request.getsession().getAttribute("list");
+1
source

questions, EL JSP.

${list}

, JSTL c:forEach:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:forEach items="${list}" var="item">
    ${item}<br />
</c:forEach>

. :

+1
source

Source: https://habr.com/ru/post/1784135/


All Articles