• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do I understand scope, redirects and JSPs correctly?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
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?

-Ben
 
Mike Himstead
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

thank you for your answer. I put together the following example:

start.jsp


target.jsp


MyBean.java


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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I always had the impression that a redirect results in a completely new request to a new web page:



It does.

Ponder how placing the bean in the session has bearing on your test.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
  • page
  • request
  • session
  • application


  • [ May 17, 2007: Message edited by: Ben Souther ]
     
    Mike Himstead
    Ranch Hand
    Posts: 178
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You've actually said the answer in you other post.

    Answer this:
    What is the difference between session scope and request scope?
     
    Mike Himstead
    Ranch Hand
    Posts: 178
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

    In session scope the bean will be available at all pages accessed within the same session. Am I correct so far?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Ben Souther
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Mike Himstead
    Ranch Hand
    Posts: 178
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic