Solved

Bean Page  scope  and Request scope in JSP page

Posted on 2000-05-05
20
773 Views
Last Modified: 2013-11-21
Can any one tell me the different between the PAGE scope of a bean and REQUEST scope of bean.

Explanation with Example woulbe be appreciated.
0
Comment
Question by:ytgprasad
  • 12
  • 5
  • 3
20 Comments
 

Expert Comment

by:agrena
ID: 2780105
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

Section 8.3

The default, page, indicates that the bean is only available on the current page (stored in the PageContext of the current page). A value of request indicates that the bean is only available for the current client request (stored in the ServletRequest object).

Hope it can help you in some fashion,

Anders
0
 

Author Comment

by:ytgprasad
ID: 2780116
This question has a deletion request Pending
0
 

Author Comment

by:ytgprasad
ID: 2780133
I am sorry for the last comment it was for other question.....
0
 

Author Comment

by:ytgprasad
ID: 2780144
to aqrena:

 can u give me an example to make out the difference......
0
 

Expert Comment

by:agrena
ID: 2780194
This question no longer is pending deletion
0
 

Expert Comment

by:agrena
ID: 2780195
If you set the scope to request, the bean will be able to use some/all of the ServletRequest methods, such as getParameter, getParameterNames etc..

Page scope will give the bean the PageContext methods, clearBuffer, flush etc..

I can't give you any examples at this point I'm afraid, but you can check out the quick syntax reference card, available at http://java.sun.com/products/jsp/syntax.pdf to get an idea of the difference.

Cheers,
Anders
0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2780245
In JSP you can use the PAGE scope and the REQUEST scope but both will have different meanings : -
 
JSP Syntax
<jsp:useBean
        id="beanInstanceName"
scope="page | request | session | application"
        {
            class="package.class" |
            type="package.class" |
            class="package.class" type="package.class" |
beanName="{package.class | <%= expression %>}" type="package.class"
        }
        {
            /> |
            > other elements </jsp:useBean>
        }
Examples
<jsp:useBean id="cart" scope="session" class="session.Carts" />
<jsp:setProperty name="cart" property="*" />
<jsp:useBean id="checking" scope="session" class="bank.Checking" >
<jsp:setProperty name="checking" property="balance" value="0.0" />
</jsp:useBean>

The scope in which the Bean exists and the variable named in id is available. The default value is page. The meanings of the different scopes are shown below:

page - You can use the Bean within the JSP page with the <jsp:useBean> element or any of the page's static include files, until the page sends a response back to the client or forwards a request to another file.

request - You can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file. You can use the request object to access the Bean, for example, request.getAttribute(beanInstanceName).
0
 

Author Comment

by:ytgprasad
ID: 2780260
as u said... I tried like this....

<jsp:useBean id="RBean" scope="request" class= "Bean" />


<p>
  IP Address <%=RBean.getRemoteHost()%>
</p>

I am using getRemoteHost() method from a request scope bean.... but this particular code is not working....what u say about this
0
 

Author Comment

by:ytgprasad
ID: 2780271
last comment was meant for agrena
0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2780298
HI ytgprasad , can you post the code please?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:ytgprasad
ID: 2780316
<%@ page language="java"  import = "Bean" %>
<jsp:useBean id="SBean" scope="session" class=  "Bean" />
<jsp:useBean id="RBean" scope="request" class= "Bean" />
<jsp:useBean id="PBean" scope="page" class= "Bean" />
<jsp:useBean id="ABean" scope="application" class= "Bean" />
<%
  SBean.setValue(SBean.getValue()+1);
  RBean.setValue( RBean.getValue()+1);  
  ABean.setValue(ABean.getValue()+1);  
  PBean.setValue(PBean.getValue()+1);    
%>

<%! private int i=0; %>

<html><body>

<p>Number of accesses <%= ++i %> </p>
<p><b> Demonstation of Scope concept
</p></b>

<p>
  S Bean value <%=SBean.getValue()%>
</p>

<p>
  R Bean value <%=RBean.getValue()%>
  IP Address <%=RBean.getRemoteHost()%>
</p>

<p>
  A Bean value <%=ABean.getValue()%>
</p>
<p>
  P Bean value <%=PBean.getValue()%>
</p>

</body></html>
0
 

Author Comment

by:ytgprasad
ID: 2780351
"Bean" is a a simple bean which has a variable and get, set methods for it......
0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2780419
what error you are getting?
0
 

Author Comment

by:ytgprasad
ID: 2781668
in response to agrene:
  I tried <%=RBean.getRemoteHost()%> it gave me error saying that getRemoteHost() is not the method of the bean - Bean

in response to yogeshr
  I tried <%=request.getAttribute(RBean)%>   then it gave error message saying this method is not in the interface of HttpServletRequest.

  I am getting confused. Please give me clear cut example to understand the difference.....
0
 

Author Comment

by:ytgprasad
ID: 2783456
I got answer, this is how I think u should use the scope

((Bean)(request.getAttribute("RBean"))).getValue()

((Bean)(pageContext.getAttribute("PBean")).setValue()


but still what I am not clear is, the uses of these two scopes in particular. Please give me any suggestion.......and corrections if any
0
 

Author Comment

by:ytgprasad
ID: 2791093
can any one tell me the ues of these two scopes page and request.......
0
 
LVL 1

Expert Comment

by:yogeshr
ID: 2791391
Hi ytgprasad, my apology for the delay in responding to your question. I was not able to access my net connection . So getting back to your question, I think if you try using the request.getattribute() the question will be solved. Regarding the difference between the two scopes there is a link http://java.sun.com/products/jsp/tags/11/tags11.html wheer you can find more info about the two scopes.
0
 

Author Comment

by:ytgprasad
ID: 2791900
yogeshr:

   Now I understood the scope from the link u provided. What I am not able to get is the use of these two scopes.
0
 
LVL 1

Accepted Solution

by:
yogeshr earned 200 total points
ID: 2791985
The scope specifies the availability of the bean for a specific request or to a client session depending on the scope value. Hence if the scope specified is  
1)page - You can use the Bean within the JSP page with the <jsp:useBean> element or any of the page's static include files, until the page sends a response back to the client or forwards a request to another file.

2)request - You can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file. You can use the request object to access the Bean, for example, request.getAttribute(beanInstanceName).

The use of the two scopes is already specified in the two points above.
0
 

Author Comment

by:ytgprasad
ID: 2795422
thank you yogeshr
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now