permaculture playing cards*
The moose likes JSP and the fly likes Quick question on session attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Quick question on session attributes" Watch "Quick question on session attributes" New topic
Author

Quick question on session attributes

John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
Hi,
I'm trying to debug a servlet and jsp page at work and need a little help, not having much experience in this field. I have tried to be concise and put 3 questions at the end which would help me if they were answered!!
What happens is this: the user clicks a html form button on a jsp page. This is posted to a servlet. The servlet then stores a session variable and dispatches the request to another jsp page, which obtains the session variable. Like this:
/* In doPost method of servlet */
Integer i = new Integer (22);
request.setAttribute ("value", i);
//now uses a request dispatcher and dispatches to another jsp page
/* in other jsp page */
<%
Integer i = session.getAttribute ("value");
%>
The problem i'm having is that I am getting the appserver giving me a compile error at runtime saying that 'session' is a variable which cannot be identified in the jsp page. I cannot understand this, I thought it was implicitly accessible.
1) Do I have to put a particular page directive at the top of my jsp file to allow the jsp to access the "session" object?
2) Should my servlet be putting an attribute in the session or the request object?
3) If there is a <useBean> tag at the top of the page, and the 'id' of that tag is set to the name of a session variable (and the type matches, too) does this mean that one can directly access the variable using the 'id' name. e.g:
//assume there is a session variable of type String called "name"
<jsp:useBean id="name" class="java.lang.String"/>
<%= name %>
will this work and print out the String?
many thanks!
john
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65


1) Do I have to put a particular page directive at the top of my jsp file to allow the jsp to access the "session" object?

No. The session variable, along with other implicit variables, are made available to the code of the page by the container. You did not mention which you were using.

2) Should my servlet be putting an attribute in the session or the request object?

Depends on the intended lifetime of the attribute. If the value is going to be used by the JSP page you are forwarding to and then no longer needed, you should put it on the request. If you need the value available after the request has finished, you should put it in the session.

3) If there is a <useBean> tag at the top of the page, and the 'id' of that tag is set to the name of a session variable (and the type matches, too) does this mean that one can directly access the variable using the 'id' name. e.g:

The scope attribute of the useBean must match where the attribute is. So scope="session" will hook up to the session attribute of the same name.
And the question you did not ask: in your JSP, the code to retrieve an attribute must cast the return value as appropriate:

bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
thanks very much!
john
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Will the following code work?

I am surprised to see the following two statements:
request.setAttribute ("value", i);
Integer i = session.getAttribute ("value");
Oh!!! Setting the attribute in request scope, and searching the attribute in session scope.
Please correct this first.
Regards,
Ganapathy,S
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
my actual working code had both setting and getting in session, i just made a mistake when typing into the post! thanks anyway.
j
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick question on session attributes
 
Similar Threads
Setting an object with c:set from sessionScope
Tiles Insert Problem
Scripting with JSTL Core tags.
My Study Notes !!!
Some Brainbench JSP Q's.....Opinions required