Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

Bean Page scope and Request scope in JSP page

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
ytgprasad
Asked:
ytgprasad
  • 12
  • 5
  • 3
1 Solution
 
agrenaCommented:
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
 
ytgprasadAuthor Commented:
This question has a deletion request Pending
0
 
ytgprasadAuthor Commented:
I am sorry for the last comment it was for other question.....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ytgprasadAuthor Commented:
to aqrena:

 can u give me an example to make out the difference......
0
 
agrenaCommented:
This question no longer is pending deletion
0
 
agrenaCommented:
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
 
yogeshrCommented:
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
 
ytgprasadAuthor Commented:
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
 
ytgprasadAuthor Commented:
last comment was meant for agrena
0
 
yogeshrCommented:
HI ytgprasad , can you post the code please?
0
 
ytgprasadAuthor Commented:
<%@ 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
 
ytgprasadAuthor Commented:
"Bean" is a a simple bean which has a variable and get, set methods for it......
0
 
yogeshrCommented:
what error you are getting?
0
 
ytgprasadAuthor Commented:
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
 
ytgprasadAuthor Commented:
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
 
ytgprasadAuthor Commented:
can any one tell me the ues of these two scopes page and request.......
0
 
yogeshrCommented:
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
 
ytgprasadAuthor Commented:
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
 
yogeshrCommented:
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
 
ytgprasadAuthor Commented:
thank you yogeshr
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 12
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now