Solved

Bean Page  scope  and Request scope in JSP page

Posted on 2000-05-05
20
774 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
cookies analysis tools 2 72
Error in @AspectJ Based AOP with Spring 2 1
servlet example 17 0
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

15 Experts available now in Live!

Get 1:1 Help Now