aspose file tools*
The moose likes JSP and the fly likes Doubt: jsp:useBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Doubt: jsp:useBean" Watch "Doubt: jsp:useBean" New topic
Author

Doubt: jsp:useBean

Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
To share Beans, we use scope attribute in jsp:UseBean. This attribute can take any of the following four values:

Page: Placed in page context and has page level access.
Application: Shared by all servlets/JSPs in same application/servlet engine.
Session: Stored in HTTPSession.
Request: Placed in ServletRequest object.

Can someone explain me the difference between all these scopes by an example. May be say,

2 applications are running in a Tomcat Server.
The pages of first application are App1P1.jsp and App1p2.jsp.
The pages of second application are App2p1.jsp and App2p2.jsp.


Thanks in advance.
_Mani


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
I read it this way ... Is it like this?

Page - available for all transactions within the entire JSP page.

Application - For all servlets of same application (or) Common to all applications, if no explicit application is defined. (how do you define an application explicitly?)

Session - Available to all transactions that happen in the same browser window.

Request - Only for that request.

Can anyone clear my doubt. Thanks in advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Manikandan Jayaraman:
I read it this way ... Is it like this?

Page - available for all transactions within the entire JSP page.

Application - For all servlets of same application (or) Common to all applications, if no explicit application is defined. (how do you define an application explicitly?)

Session - Available to all transactions that happen in the same browser window.

Request - Only for that request.

Can anyone clear my doubt. Thanks in advance.


Sounds like you've got it.
  • "page" is the shortest and the most local. It is bound to the pageContext object for the current request.
  • "request" also exists only for a single request but allows different components within this request (look up: MVC or requestDispatcher) access to objects bound to it.
  • "session" allows objects to be stored and retrieved across multiple requests for this user's session (as maintained with the use of a session cookie for the current browser instance).
  • "application" or context scope makes objects bound to it accessible for all requests within the current application.


  • (how do you define an application explicitly?)
    Take a look at SRV3.1 from the Servlet Spec (link in my signature). It defines and explains the concept of a contex in a servlet app.
    In short, you can have multiple, isoltated applications running under one app server. In Tomcat for instance, each context (application) lives in a separate directory under the webapps directory. (there are other ways to deploy also, but worry about that later). Each app can be configured differently via the deployment descriptor (a file named web.xml under the WEB-INF directory which is just under the root directory for the context).
    Each of the examples on my site (link below) is a separate context.
    [ May 14, 2005: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Manikandan Jayaraman
    Ranch Hand

    Joined: Sep 15, 2004
    Posts: 230
    Hi Ben!

    Thanks a ton for your clarification. I got things clarified now. And Yeah ... will go through the servlet spec. that you have given and will get back to you in case of further queries.

    Thanks again!

    Regards,
    Mani
    Manikandan Jayaraman
    Ranch Hand

    Joined: Sep 15, 2004
    Posts: 230
    Opening back a chain after a long time. But This time, I have read 70% of Servlet Spec and Worked on Servlets too. Now getting back again into JSPs.

    Please confirm if the below is right!

    A Bean with a Scope of "Page" - the scope of this bean is the shortest. This bean will not be available if request is forwarded to other JSPs. Is this right?

    Where as, if Scope of "request", the bean is available to all pages where this request is forwarded too.

    Is this right? Please advise.

    Thanks and Regards,
    Mani
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Originally posted by Manikandan Jayaraman:
    Opening back a chain after a long time. But This time, I have read 70% of Servlet Spec and Worked on Servlets too. Now getting back again into JSPs.

    Please confirm if the below is right!

    A Bean with a Scope of "Page" - the scope of this bean is the shortest. This bean will not be available if request is forwarded to other JSPs. Is this right?

    Where as, if Scope of "request", the bean is available to all pages where this request is forwarded too.

    Is this right? Please advise.

    Thanks and Regards,
    Mani


    Right Mani.


    Groovy
    Manikandan Jayaraman
    Ranch Hand

    Joined: Sep 15, 2004
    Posts: 230
    Thanks a lot for your clarification

    Regards,
    Mani
    Wagner Danda Da Silva Filho
    Ranch Hand

    Joined: Mar 21, 2003
    Posts: 80
    Originally posted by Manikandan Jayaraman:
    A Bean with a Scope of "Page" - the scope of this bean is the shortest. This bean will not be available if request is forwarded to other JSPs. Is this right?


    According to this site Forward vs Redirect you can see that a "Redirect" (corrected) actually makes a new request, and the beans available in the old request are lost.
    [ October 25, 2005: Message edited by: Wagner Danda ]

    SCJP, SCWCD
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61653
        
      67

    you can see that a "Forward" actually makes a new request, and the beans available in the old request are lost


    That is completely and utterly incorrect.

    A forward does not create a new request; it passes the current request on to another resource.
    [ October 25, 2005: Message edited by: Bear Bibeault ]

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Vishnu Prakash
    Ranch Hand

    Joined: Nov 15, 2004
    Posts: 1026
    If we store an attribute in application scope and use sendRedirect to direct to a different context(application) is it possible to access that attribute in the next context.


    Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
    Wagner Danda Da Silva Filho
    Ranch Hand

    Joined: Mar 21, 2003
    Posts: 80
    Hey, you are right, I sweeped the concept "Forward" with "Redirect"

    So, a Forward keeps the context but a Redirect don't. Just be aware of this while coding.
    [ October 25, 2005: Message edited by: Wagner Danda ]
    Pradeep bhatt
    Ranch Hand

    Joined: Feb 27, 2002
    Posts: 8919

    Originally posted by Vishnu Prakash:
    If we store an attribute in application scope and use sendRedirect to direct to a different context(application) is it possible to access that attribute in the next context.


    No.
     
    Don't get me started about those stupid light bulbs.
     
    subject: Doubt: jsp:useBean