GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Page scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Page scope" Watch "Page scope" New topic
Author

Page scope

Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
This is a question from withmilk.com site...
Which of the following implicit JSP objects have page scope?
The ans provided is response and config.
But I am not convinced that config has a page scope because servlet container can use it to pass information to a servlet during initialization for application running over multiple pages.
Can anyone clarify the real answer please?
which other objects have page scope?
Thanks in advance.


Rajesh(Bangalore,India)<br />SCJP2, SCWCD, SCEA, IBM-XML, UML-OOAD, IBM-Enterprise Connectivity with J2EE.
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
This is confusing question to me.
response should have Request Scope, right? Response always go with Request in whatever forward, include. But maybe, Response "can" be in Page scope.
config, also, "can" be in Page scope, but if multiple instance of that servlet is generated, then config does not have any valid JSP scope?
what about pageContext, I thought this guy definitely have Page scope?
Kyle


Kyle Tang<br />SCJP 91<br />SCWCD 96<br />SCBCD 95
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
sorry, I think it is improper to give config a scope.
config is attached to the servlet instance, that servlet instance could be accessed multiple times, so config is not in page scope, and not in request scope either.
config is not in application scope or session scope, obviously.
For the original question, I think "out" is also in the Page scope. right? so the answer should be: out, pageContext, (request, response)?
Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
I checked with the JSP1.2 spec and it mentions that implicit objects config and response have page scope.
/*
-Pathak
But I am not convinced that config has a page scope because servlet container can use it to pass information to a servlet during initialization for application running over multiple pages.
*/
I looked at the implementation class of a JSP and
found that config is a private instance member of the class.
Now what does it mean when we declare an object to have page scope? It means the object is persistent as long as the JSP page instance is not unloaded. Config object has some attributes which it takes from web.xml(init-params). More attributes can be added after initialization. All these attributes retain thier values till the JSP page instance is active, and hence it has page scope.
Consider the case of session scope. An object declared with session scope can be accessed by all the servlets of an application. This means that a refrence to the session object is accessible from all the servlets of an application.
If there's only one servlet or JSP in an application then page scope and session scope are essentially the same thing(except the fact that session object gets invalidated after some time where as page objets remain as long as JSP instance is active).
The point is "page" scope can be assumed as storing the object as an instance member of a JSP
and its specific to a JSP. config object is a perfect fit for this as it is specific to a JSP.
I'm not very clear as to why response object also has a page scope.
will try to dig a little more.
any expert inputs from others will be great.
thanks
Ali


SCJP2, SCWCD, XML, OOAD<br />Kolkata, India
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
Thanks Ali!
I checked the spec, it seems pretty much all implicit objects are "defined" to be in page scope. except request, application.
but I disagree with you on one thing, to be in page context does not mean the value should be valid until the servlet instance in unloaded. Page scope is not "servlet instance scope".
To be in page scope means, the value is accessible from pageContext, and valid for this particuly request.
SCWCD Study Kit mentioned this, and it says though JSP spec defined those implicit variables to be in page scope, they are not logically restricted to that scope. (page 222)
thanks
Kyle
Raj Paul
Ranch Hand

Joined: Jul 09, 2002
Posts: 77
Hi,
Even i not convinced with scope of config object. Even logically it should be request scope, because we get config object only inside _jspService method. so why not request scope ?

Thanks & Regards
Raj Paul
Tauqueer Ali
Ranch Hand

Joined: Sep 05, 2001
Posts: 53
- Paul
Even i not convinced with scope of config object. Even logically it should be request scope, because we get config object only inside _jspService method. so why not request scope ?

here's a part of implementation class of a JSP
abstract class ExampleHttpSuper implements HttpJspPage {
private ServletConfig config;
final public void init(ServletConfig config) throws ServletException {
this.config = config;
jspInit();
public void jspInit() {
}
public void jspDestroy() {
}
final public ServletConfig getServletConfig() {
return config;
}
We get config in _jspService() method by a call to getServletConfig(). Please note that config is an instance member of the class whereas request is just a local variable in service method.
One thing we need to convince ourselves is the literal meaning of scope. And declaring an object to have page scope is nothing but making it an instance variable of the class, which can be seen in the above code.
-Kyle
but I disagree with you on one thing, to be in page context does not mean the value should be valid until the servlet instance in unloaded. Page scope is not "servlet instance scope".
To be in page scope means, the value is accessible from pageContext, and valid for this particuly request.
SCWCD Study Kit mentioned this, and it says though JSP spec defined those implicit variables to be in page scope, they are not logically restricted to that scope. (page 222)
If its mentioned in SCWCD Study Kit, it may be true but my doesn't it contradict with the JSP Spec?. As u mentioned, to be in page scope means the value is valid for a particular request, but thats exactly what a request scope is. If thats the case then what is the diffrence between page and request scope.
Raj Paul
Ranch Hand

Joined: Jul 09, 2002
Posts: 77
Hi,
what we get through this.getServletConfig() gives you the config object of super class. In servlet we will get GenericServlet class config object.
But in JSP, the variable we get can only be used in scriplets & expressions. why is it so ? we wont get in other scriplets(declaration).
so why not request scope or pageContext scope (because pageContext save these variables even then pagecontext scope is request scope)?
In Orion Server the created code is as follows

PageContext pageContext = JspFactory.getDefaultFactory().getPageContext( this, request, response, null, true, JspWriter.DEFAULT_BUFFER, true);
HttpSession session = pageContext.getSession();
int __jsp_tag_starteval;
ServletContext application = pageContext.getServletContext();
JspWriter out = pageContext.getOut();
_just1 page = this;
ServletConfig config = pageContext.getServletConfig();
These lines are in _jspService method.
Raj Paul
Rajesh Pathak
Ranch Hand

Joined: Aug 15, 2002
Posts: 56
--------------------------------------------------
Quote: Tauqueer Ali
Config object has some attributes which it takes from web.xml(init-params). More attributes can be added after initialization. All these attributes retain thier values till the JSP page instance is active, and hence it has page scope.
--------------------------------------------------
This statement logicaly proves that config will have page scope, as it attributes have influence only within that page.
Let�s us consider the feature of response implicit object (I'm still puzzled!!)
It is used to store ServletResponse and assists a servlet in sending a response to the client.
Let's say in a servlet it has a particular value of contentType, contentLength, BufferSize etc to be used in sending response to the client. Then we use
RequestDispatcher.forward(ServletRequest request, ServletResponse response)
Now my question is, as I am sending the reference of the response object to another �target� page, it will have its influence there. So how come it has page scope?
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
But I am not convinced that config has a page scope because servlet container can use it to pass information to a servlet during initialization for application running over multiple pages.

Although config can pass information to a servlet during initialization for application running over multiple pages, the information is always the same for all pages. Thus I would think it has the page scope.


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
 
GeeCON Prague 2014
 
subject: Page scope