This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi Guys, We know there're 3 scopes in servlets but 4 scopes in jsp. Could someone can tell me the differiences and relationships between the scopes of the two components? We know page scope is unique to jsp. It seems to me it could combine with request scope. Thanks,
JSP.2.8.2 Objects and Scopes A JSP page can create and/or access some Java objects when processing a request. The JSP specification indicates that some objects are created implicitly, perhaps as a result of a directive (see Section JSP.2.8.3, “Implicit Objects). Other objects are created explicitly through actions, or created directly using scripting code. Created objects have a scope attribute defining where there is a reference to the object and when that reference is removed. The created objects may also be visible directly to scripting elements through scripting-level variables (see Section JSP.2.8.3, “Implicit Objects). Each action and declaration defines, as part of its semantics, what objects it creates, with what scope attribute, and whether they are available to the scripting elements. Objects are created within a JSP page instance that is responding to a request object. There are several scopes: • page - Objects with page scope are accessible only within the page where they are created. All references to such an object shall be released after the response is sent back to the client from the JSP page or the request is forwarded somewhere else. References to objects with page scope are stored in the pageContext object. • request - Objects with request scope are accessible from pages processing the same request where they were created. References to the object shall be released after the request is processed. In particular, if the request is forwarded to a resource in the same runtime, the object is still reachable. References to objects with request scope are stored in the request object. • session - Objects with session scope are accessible from pages processing requests that are in the same session as the one in which they were created. It is not legal to define an object with session scope from within a page that is not session-aware (see Section JSP.2.10.1, “The page Directive). All references to the object shall be released after the associated session ends. References to objects with session scope are stored in the session object associated with the page activation. • application - Objects with application scope are accessible from pages processing requests that are in the same application as they one in which they were created. Objects with application scope can be defined (and reached) from pages that are not session-aware. References to objects with application scope are stored in the application object associated with a page ctivation. The application object is the servlet context obtained from the servlet configuration object. All references to the object shall be released when the runtime environment reclaims the ServletContext. A name should refer to a unique object at all points in the execution, that is all the different scopes really should behave as a single name space. A JSP container implementation may or may not enforce this rule explicitly due to performance reasons.
There is a natural mapping from the servlet scopes to the scope attributes of objects accessible in JSP pages. The scope attribute of "page" is unique and has no counterpart.
We know page scope is unique to jsp. It seems to me it could combine with request scope.
I am not sure by what you mean by this. Were you saying that an object defined with page scope in A.jsp, set as an attribute to a request, and forwarded to B.jsp, is accessible there? It wouldn't be. -anthony
Well, in a sense "page scope" is available in servlets. Only we call it "local variable" (there are differences, of course, but the scope is really the same). - Peter
Joined: Jan 21, 2002
Thanks guys, Great inspiration
Joined: Mar 22, 2002
Well, in a sense "page scope" is available in servlets. Only we call it "local variable" (there are differences, of course, but the scope is really the same).
I thought about that too, Peter, but a local variable will only have scope within a specific method it was defined, e.g service(), doGet(). etc.. If we think of a servlet as a "page" perhaps a servlet instance variable would be appropriate, yes? BTW would you happen to know a site where I can find physics-related Java APIs? -anthony
Peter den Haan
Joined: Apr 20, 2000
Originally posted by Anthony Villanueva: I thought about that too, Peter, but a local variable will only have scope within a specific method it was defined, e.g service(), doGet().
True - but remember that a JSP is in fact just a single huge method (_jspService), so there is no difference here. For example, if you define a <%! method() %> inside a JSP the page-scoped variable will be out of scope unless you pass in the PageContext and use that to access it. In fact, the fact that a page-scoped JSP variable is also bound in the PageContext object is the difference between it and a simple local variable.
If we think of a servlet as a "page" perhaps a servlet instance variable would be appropriate, yes?
Not quite; an instance variable is shared between multiple threads, for example.
BTW would you happen to know a site where I can find physics-related Java APIs?
No; for my physics work, I was forced to learn (ugh yuk) Fortran (yech) - Peter [ April 07, 2002: Message edited by: Peter den Haan ]