3

I have a problem with my program.

I have a servlet; in this servlet save the session attribute

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

Now the attribute is a String and not an ArrayList. In fact when i try to do:

request.getsession().getAttribute(list)

is a string and not an Array.

I want an Array.

Thanks

0

3 Answers 3

5

You have to cast when you get the attribute from the session like this:

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

And the attributes in the session are stored in a map, that is why the key you used is a String and you have to use a string to retrieve the value.

Sign up to request clarification or add additional context in comments.

6 Comments

@zp26 What is the problem with it?
Multiple markers at this line - Type safety: Unchecked cast from Object to ArrayList<Integer>
@zp26 Fixed. Also org.life.java - Jigar Joshi had it correct all along.
@zp26 this is not a problem.. Just change the cast to ArrayList<Integer> list = (ArrayList<Integer>)object and the "Unchecked" will disappear. "The local variable.." that means you are not using the variable along the rest of the code (or sometimes it bugs when you use it inside some conditions, cant remember very well wich ones).
Sorry but the Unchecked cast don't disappear.
|
1

session.getAttribute(..) returns Object

You will have to cast it like

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

Comments

1

As answered in your previous questions, just access it by EL in JSP.

${list}

If you want to iterate over it, use JSTL c:forEach:

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

See also:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.