Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About jsp:useBean !!!!

 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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�.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks
 
Amitkumar Dhama
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this question from cert Gear mock, two jsp pages were given there

first one:
"First.jsp"

-----------------------
<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:
"Second.jsp"

-----------------------
<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
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amithkumar:
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.*" %>
<html>
<body>

<%
person lclperson = new foo.person();
lclperson.setName("YourName");
pageContext.getSession().setAttribute("person", lclperson);

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

%>
<br>

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

<br>
Done.
</body>
</html>

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.

Narendra:

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
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic