Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt: jsp:useBean

 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
     
    Manikandan Jayaraman
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Manikandan Jayaraman
    Ranch Hand
    Posts: 230
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot for your clarification

    Regards,
    Mani
     
    Wagner Danda Da Silva Filho
    Ranch Hand
    Posts: 80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64973
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    Vishnu Prakash
    Ranch Hand
    Posts: 1026
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Wagner Danda Da Silva Filho
    Ranch Hand
    Posts: 80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8927
    Firefox Browser Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic