I pose this question to make sure that my understand of JSPs (<jsp:useBean> in specific) and redirects is correct.
Given a jsp (start.jsp) that declares a bean in session scope. After declaration I run a static init method initializing some of the bean's members from within a scriptlet part.
Given I'm forwarded to a second jsp (second.jsp) that has the very same bean declaration I expect the second JSP to takeover the first bean in its initialized state.
Is this correct? If so, what if I use a redirect to access the second JSP? Will the second.jsp's bean a takeover from the previous page?
It would have taken you about the same amount of time to write two test JSPs that it took you to put your post together. Your answer, undoubtedly, would have come faster.
Have you tried this.
Did it work as expected?
Also, there is a link to the JSP spec in my signature.
In that spec there is a very well written section on the useBean action that explains exactly how it should work in a spec compliant container.
Why not give it a shot and come back to us with your answers?
thank you for your answer. I put together the following example:
So basically I threw this at a local Tomcat and got to see a "target.jsp" titled page with "Member is initialized!" staring at me. It seems that it doesn't make any difference to use a redirect or a forward. To be honest, this was NOT the result I expected. I always had the impression that a redirect results in a completely new request to a new web page: the client (the browser) sends the same data to a new page, and the server treats this new request as if it never saw the client before.
The basic reason I ask is that I experience the behavior I just described with a more complex bean. The bean's member is a session object representing a session the web app has created after logging in at another backend server. The log in procedure at start.jsp takes places and creates the session object member, and after that it redirects to target.jsp. When accessing this member in target.jsp, it's null, so it seems I have a new object at hand. When using <jsp:forward> the member is initialized.
There is more to say about the environment this takes place. The first request is proxied to ensure a safe environment the webapp is accessed from. So it might be that the redirect in connection with the proxy has unforeseen side-effects, especially with the session object and the connection to the backend server it represents.
Originally posted by Mike Himstead:
To be honest, this was NOT the result I expected. I always had the impression that a redirect results in a completely new request to a new web page: ....
The scoped objects to which you can bind a variable are:
[ May 17, 2007: Message edited by: Ben Souther ]
In session scope the bean will be available at all pages accessed within the same session. Am I correct so far?
Close. The scoped variable (correct term for a bean placed in a scoped context) will be available to all resources participating in the same request. Typically, the scoped variable is set up by a servlet controller and is reference on a page forwarded to by that cotroller. But the scoped variable will also be available to other servlets that might be forwarded to, pages included by the JSP page, and any page forwarded to by a JSP page (rare in a properly structured web app).
Originally posted by Mike Himstead:
If I declare a bean with request scope it will be available at the first page that gets the request, the next page gets a new request and won't have access to the bean.
Once the response is sent to the client, be it a redirect or not, the current request goes out of scope, and with it, all of its scoped variables.
All resources participating in the session, pages and servlets alike, will have access to session scoped variables.
In session scope the bean will be available at all pages accessed within the same session.
Originally posted by Bear Bibeault:
Typically, the scoped variable is set up by a servlet controller and is reference on a page forwarded to by that cotroller. But the scoped variable will also be available to other servlets that might be forwarded to, pages included by the JSP page, and any page forwarded to by a JSP page (rare in a properly structured web app).
This is the preferred way of doing things now (best practice).
Your posted code is using the <useBean .../> action (not really needed in modern scriptless pages with servlet controllers).
In the case of pages using the <useBean ../> directive, you are correct, if you declare the bean to have session scope, the first and all pages participating in that session will have access to that bean.
This is a little bit over simplified but useBean is shorthand for:
Look for bean with the given id, in the given scope.
If it exists, assign it to a page variable matching the given id.
If not, then create it, bind to the given scope and then assign it to a page variable matching the given id.
So, yes, the first page hit will have that bean available to it; as will all subsequent pages participating in the same session if the bean is bound to session scope.