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
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:
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
posted 16 years ago
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 have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad: