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.
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 ]
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 ]
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.