File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes About jsp:useBean !!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "About jsp:useBean !!!!" Watch "About jsp:useBean !!!!" New topic

About jsp:useBean !!!!

Amitkumar Dhama
Ranch Hand

Joined: Feb 14, 2005
Posts: 56
Hi all,

can anybody tell me what will happen if I am using

<jsp:useBean id="person" type="foo.person"/>

Standard action then, object of that type must be present with the name ID and in the specified scope, since I am not using scope attribute here.

1�will it be searched only in page scope or in all the scopes please clarify this to me.

Suppose I am having a:
One request scoped attribute �person�.

One session scoped attribute �person�

One application scoped attribute �person�

No page-scoped attribute is available.
Please clarify..

Thanking in advance�.

SCJP 1.4, SCWCD 1.4, M.Tech(CSE)
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 951

It will not search in all scope. If the class/beanName attributes are not specified the object must be present in the given scope.


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Amitkumar Dhama
Ranch Hand

Joined: Feb 14, 2005
Posts: 56
I got this question from cert Gear mock, two jsp pages were given there

first one:

<jsp:useBean id="bean" class="bean.MyBean" scope="session"/>
<jsp:setProperty name="bean" property="name" value="first page"/>
<jsp:include page="second.jsp"/>
<jsp:getProperty name="bean" property="name">

and the IInd one is:

<jsp:useBean id="bean" class="bean.MyBean" scope="application"/>
<jsp:setProperty name="bean" property="name" value="second page"/>

i tried this under the tomcat i am getting output as

Second page

but mock says that there will be compile time error.

i dont know why i am getting this output how getproperty standerd action is searching the property.

please clarify this to me
I am going to take the test 6th of this month.
M Jairam

Joined: Nov 04, 2005
Posts: 22
The following is my observation:

1. If you don't specify the scope attribute in <jsp:useBean..>, then the scope defaults to "page". (Ref: HFSJ top of page 355).
So, in the situation where you don't have a scope attribute with
<jsp:useBean id="person" type="foo.person"/>
the scope defaults to page, and the statement is same as having:
<jsp:useBean id="person" type="foo.person" scope="page"/>
In summary, it will not search or find in any other scope (unlike an EL attribute where it will search in all four scopes).

You can try this with the following example:

<%@ page import="foo.*" %>

person lclperson = new foo.person();
pageContext.getSession().setAttribute("person", lclperson);

// pageContext.setAttribute("person", lclperson);


<jsp:useBean id="person" type="foo.person" />
<jsp:setProperty name="person" property="name" value="SomeName" />
<jsp:getProperty name="person" property="name"/>


The java bean:

package foo;

public class person {

private String name = null;

public person() {
name = "";

public String getName() {
return name;

public void setName(String inname) {
name = inname;


2. For the second question, your observation is the same as mine, that is it does print "Second Page" - this only if I ignore the following syntax error in your post:
<jsp:getProperty name="bean" property="name">
is not properly terminated, if above line is used as is, you will get compile error.

Hope this helps.


What is the meaning of your reply above? I did not get it. "It will not search in all scope." is correct. However, the next sentance you mention "class/beanName" - what has this got to do with scope - I don't think scope depends on these attributes in anyway. Also, what did you mean by "given scope"? May be you ment the default scope - "page"...
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
<jsp:useBean id="person" type="foo.person"/>

you would get a big fat instantiation error if u dont have a bean called "person" in ur page scope.(class attribute is not specified in the tag)

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
I agree. Here's the link:
subject: About jsp:useBean !!!!
It's not a secret anymore!