sukumar wrote:
>
> hi,
> pls tell me the exact difference between the "page" and "request" scope
> of the bean.
> (with an example)
When a bean is defined in "page" scope, it's accesible *only* during JSP
page compilation and nowhere else. So, JSP page can refer to the bean
only within itself. In case of "request" scope, a bean is accessible in
*any* other pages until request is valid. Let see at some examples:
1. Page scope
<useBean id="bean" class="my.package.Bean" scope="page" />
<jsp:getProperty name="bean" property="records" />
In that case, bean can be accessed *only* within the page. Thus, it's
not possible to access the bean outside of the page, i.e. in the
following page after forwarding request, for example.
The solution can be used when you *don't* set any parameters and is used
to retrieve some, e.g. from database. Thus, when I'd define the another
page which could participate in a request, the bean will be recreated as
it's not available yet. Let's see it:
<jsp:useBean id="bean" class="my.package.Bean" scope="page">
<jsp:setProperty name="bean" property="name" value="Message from the
first page" />
</jsp:useBean>
<jsp:forward page="/test.jsp" />
and in test.jsp:
<useBean id="bean" class="my.package.Bean" scope="page" />
<jsp:getProperty name="bean" property="name" />
Now, you get a default value from getName() method - no "Message from
the first page" in a browser.
2. Request scope
When you change the above example to "request" scope,
<jsp:useBean id="bean" class="my.package.Bean" scope="request">
<jsp:setProperty name="bean" property="name" value="Message from the
first page" />
</jsp:useBean>
<jsp:forward page="/test.jsp" />
and in test.jsp:
<useBean id="bean" class="my.package.Bean" scope="request" />
<jsp:getProperty name="bean" property="name" />
that'll work as the bean will be created in the former page and after
forwarding a request, the following page *can* access it. You should see
"Message from the first page" in a browser.
> s.sukumar
Jacek Laskowski
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets