File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Bean standard action doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Bean standard action doubt " Watch "Bean standard action doubt " New topic
Author

Bean standard action doubt

Sue Pillai
Ranch Hand

Joined: Nov 04, 2004
Posts: 89
I came across this code in HF'servlets and JSPs (pg:358) and am confused

TestBean.jsp

<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>

The html that calls this jsp is:

<html><body>
<form action="TestBean.jsp">
Name: <input type="text" name="userName">
...and so on
...
</form>
</body></html>


I understood this as:

"a request parameter is being set to a property of a new bean attribute in the page scope."

Am I correct?

What happens if I used a <jsp:getProperty> with a request scope? Is null printed on the browser, or does an exception occur?

Thanks.


Sue

SCJP 1.2
SCWCD 1.4
SCBCD 5.0
satheesh krishnaswamy
Ranch Hand

Joined: Mar 17, 2004
Posts: 137
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>
a request parameter is being set to a property of a new bean attribute in the page scope


I think,if we use body,i.e jsp:setProperty within jsp:useBean,a New Bean is created and the values are set.

Hence if we use jsp:getProperty before setting the values,we will get only Null and not an Exception.The Reason I could give is the bean will created in this case but only the values are not set.Exception will be thrown in the case only when the Bean is not found in the page scope.
Hence i feel only Null will be the result.

Can anyone correct me if I am Wrong!!

Thanks..


Regards,<br /> <br />Satheesh.K<br />-------------<br />SCJP 1.4<br />SCWCD 1.4
P Chittaranjan
Ranch Hand

Joined: Feb 09, 2006
Posts: 47
Hi,

We don't have any scope attribute in <jsp:setProperty> and <jsp:getProperty>. For using the scope we have declare in <jsp:useBean> tag.

when you click submit button on the html page, it will invoke TestBean.jsp.

In TestBean.jsp we are creating a object of the java bean. If we will mention the scope there in <jsp:useBean>, it will put the created object into respective scope otherwise it will be in page scope.

So using the <jsp:getProperty> in this example has no relation with the scope.

The following two will give you the same result.

1.TestBean.jsp

<jsp:useBean id="person" type="foo.Person" class="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>


<jsp:setProperty name="person" property="name" />


2.TestBean.jsp

<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>


<jsp:setProperty name="person" property="name" />

It will not print null.

Using scope means, you are specifying where to put the bean object after creating it.
<jsp:getProperty> will look for it starting from the page scope to application scope.

Thanks
Chittaranjan


Thanks,<br />Chittaranjan<br />SCJP 5.0,SCWCD1.4,SCBCD 5
Ravi Appana
Greenhorn

Joined: Aug 25, 2004
Posts: 13

<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name" param="userName" />
</jsp:useBean>

I understood this as:

"a request parameter is being set to a property of a new bean attribute in the page scope."

Am I correct?


This code means, if a bean is not present in the page scope(which is the default, if not specified in useBean), create it in the page scope, and for the bean property "name", call the setName(String) method with the value corresponding to the request parameter name "userName".

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

is same as

<jsp:useBean id="person" type="foo.Person" class="foo.Employee" scope="page" />


Ravi Sankar Appana<br />SCJP 1.4
Sunjeev Shetty
Ranch Hand

Joined: Aug 21, 2005
Posts: 48
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("username") %>/>
</jsp:useBean>

Does this also create an instance in page scope?
P Chittaranjan
Ranch Hand

Joined: Feb 09, 2006
Posts: 47
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("username") %>/>
</jsp:useBean>
Does this also create an instance in page scope?


Hi Sanjeev,

Before explaining any thing I will take the jsp:setProperty as bellow because you have missed one " in the tag.
<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("username") %>"/>

Now we can come to the Topic:

<jsp:useBean id="person" type="foo.Person" class="foo.Employee">

This is the tag which decides where to create the bean object depending on the scope attribute.
Scince we have not mentioned the scope attribute, the default is page scope. So it will create the bean object in page scope.

Using request object in jsp:setProperty will not craete the bean object in request scope.
In fact <jsp:setProperty> and <jsp:getProperty> are there to use the object created by <jsp:useBean> Tag.


<jsp:useBean id="person" class="foo.Employee">

Container will create a object like this

foo.Employee person=new foo.Employee();

Then it will add the object into pageContext like bellow.

pageContext.setAttribute("person", person, PageContext.PAGE_SCOPE);


<jsp:setProperty name="person" property="name" value="<%=request.getParameter("username") %>/>

setProperty is there to set the property(member variable) of a object.
Which object?
What ever we have mentioned in "name" attribute (name="person").In this case the object is person.

Now the question is setPropery doesn't know where the object persion is.

So it will search for the object "person" starting from PAGE_SCOPE to APPLICATION_SCOPE.
If it will find the object in PAGE_SCOPE ,it will stop the searching there( means, it will not go to REQUEST,SESSION and APPLICATION SCOPE).Then it will use that object to set the property.


Hope this will help you.Please let me know if you need more explanation.

Thanks
Chittaranjan
Sunjeev Shetty
Ranch Hand

Joined: Aug 21, 2005
Posts: 48
Thanks for the wonderful explanation Chittaranjan.
Ranjani Theyagarajan
Greenhorn

Joined: Feb 08, 2006
Posts: 19
ContainerPage.jsp
===================
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("username") %>/>
</jsp:useBean>


includedPage.jsp
================
<jsp:useBean id="person" type="foo.Person" class="foo.Employee" scope="request">
<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("username") %>/>
</jsp:useBean>

If we include includedPage.jsp in ContainerPage.jsp what will be the outcome? Will it work?
 
wood burning stoves
 
subject: Bean standard action doubt