wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes scopes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "scopes" Watch "scopes" New topic
Author

scopes

tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
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,


SCJP2, SCWCD
Michael Sunny
Greenhorn

Joined: Apr 05, 2002
Posts: 5
as i understand, objects available in a page scope may not be available when it forwards to another resource. but they are available if they are scoped as "request"
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
According to the Servlet 2.3 specs:
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
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
Thanks guys,
Great inspiration
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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 ]
 
Consider Paul's rocket mass heater.
 
subject: scopes
 
Similar Threads
JSP usebean, getproperty, setproperty and scopes
Difference between parameters and attributes in Servlets & JSP
Tld Variable Scopes
Servlet scopes vs. bean scopes in Spring
scopes in the jsp