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: ....
Hint: The scoped objects to which you can bind a variable are:
[ May 17, 2007: Message edited by: Ben Souther ]
Joined: Apr 12, 2006
I'm afraid your hints confuse me even more, I can't see a difference between my example and the web app that shows a different behavior. In basic a JSP creates a bean, puts it into the session, initializes a few members and redirects to a second page that shall make use of this bean. My example does so, the other web app does only when forwarded, not when redirected.
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.
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).
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.
In session scope the bean will be available at all pages accessed within the same session.
All resources participating in the session, pages and servlets alike, will have access to session scoped variables.
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.
Joined: Apr 12, 2006
Thanks to both of you for your replies. It seems that there must be another factor that gets my bean out of session scope when using a redirect, but not when using forwarding. I'm suspicious about the proxy, unfortenately I've no experience with proxies.