File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes session attribute problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "session attribute problem" Watch "session attribute problem" New topic

session attribute problem

Joyce Derzaph

Joined: Mar 18, 2004
Posts: 14
I'm having trouble passing attributes back and forth between a servlet and a jsp.
I set the attribute in the servlet then forward the request and response to a jsp. The attribute shows up in the servlet but comes up null in the jsp.
Here is the code sections:

session.setAttribute("trans",t); // t was initialized earlier
RequestDispatcher rd = req.getRequestDispatcher("/mainTill.jsp");

<% double total = 0;
Transaction t;
t = (Transaction)request.getAttribute("trans");
<%= t.toString() %>
If anyone has any ideas I'd appreciate it. This is driving me nuts!

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Joyce Derzaph:

session.setAttribute("trans",t); // t was initialized earlier
t = (Transaction)request.getAttribute("trans");

You are setting 't' as a session attribute, but then you are trying to retrieve it as a request attribute.

either use setAttribute and getAttribute with the session object, or use both with the request object

The future is here. It's just not evenly distributed yet. - William Gibson
Sonny Gill LinkedIn
jaya jaya

Joined: May 17, 2003
Posts: 8
Use session.getAttribute("tran")
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

"jaya jaya",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Forum Bartender

[Asking smart questions] [About Bear] [Books by Bear]
Joyce Derzaph

Joined: Mar 18, 2004
Posts: 14
Doh! I found it last night. Thanks. I shouldn't program late at night.

I agree. Here's the link:
subject: session attribute problem
It's not a secret anymore!