This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick question on session attributes

 
John Summers
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
John Summers
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much!
john
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my actual working code had both setting and getting in session, i just made a mistake when typing into the post! thanks anyway.
j
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic