Problem Retrieving Session Attributes

Hi,

I have a web application where the user hits a servlet which does some processing and puts attributes into the session and then forwards the request on to a JSP. The JSP then retrieves the attributes from the session.

I can retrieve session attributes using EL but not scriptlets. For example, the the string is proprerly retrieved/displayed for this:

<c:out value="${sessionScope.TEXT_ACTIVE}" />

but not for:

<%

Integer maxItems =new Integer((String)session.getAttribute("itemsPerPage"));

int maxPageItems = maxItems.intValue();

%>

I get a NumberFormatException because the attribute retrieved is null. I've confirmed in my servlet code that the attribute is set before I do the forward.

I've used scriptlets to retrieve session attributes before, and I'm not doing anything differently (I think). As far as I know, it should "just work". Does anyone have any thoughts on what might be wrong?

All help appreciated.

[1125 byte] By [ShelliOa] at [2007-11-26 14:59:08]
# 1

Usually it is mis-naming error, spelling or capitalization error between the attribute name added to the session and the attribute name retrieved from the session.

Another possiblility is that you are putting a null value in the session or after setting the session attribute you are nulling the object.

As a side note what is wrong with using the Integer.parseInt(String) method to convert a string to an int. It eliminates the need to create an Integer object.

tolmanka at 2007-7-8 8:47:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

It should work even if you try to set the session value with c:set and try to retrieve it with session.getAttribute.

Here's a small example of what I tried and it worked:

index.jsp

~~~~~~~

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head><title></title></head>

<body>

<%

session.setAttribute("firstAttribute","firstValue");

%>

<c:set var="secondAttribute" value="secondValue" scope="session"/>

</body>

</html>

page2.jsp

~~~~~~~~

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title></title></head>

<body>

<%=session.getAttribute("firstAttribute")%>



<%=session.getAttribute("secondAttribute")%>

</body>

</html>

appy77a at 2007-7-8 8:47:56 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...