Link to home
Start Free TrialLog in
Avatar of ytgprasad
ytgprasad

asked on

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.
Avatar of agrena
agrena

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
Avatar of ytgprasad

ASKER

This question has a deletion request Pending
I am sorry for the last comment it was for other question.....
to aqrena:

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you yogeshr