• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Bean standard action doubt

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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..
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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" />
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the wonderful explanation Chittaranjan.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic