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

jsp:setProperty

 
Shini Akshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried following code to test the usage of jsp:setProperty tag

index.jsp

<html>
<head>

<title>JSP Page</title>
</head>
<body>
<form method=post action="personServlet">
<input type="submit" name="btnSubmit" value="Submit" >
</form>
</body>
</html>

personServlet

foo.Person p=new foo.Person();
p.setName("cc");

sc=getServletContext();
sc.setAttribute("person",p);
RequestDispatcher d=request.getRequestDispatcher("abc.jsp");
d.forward(request,response);

abc.jsp
<body>
<jsp:useBean id="person" class="foo.Person" scope="application" />
<jsp:setProperty name="person" property="name" value="ss" />
<jsp:getProperty name="person" property="name" />
<%
foo.Person p=(foo.Person)application.getAttribute("person");

out.println(p.getName()); %>

<jsp:setProperty name="person" property="name" value="pp" />
<jsp:getProperty name="person" property="name" />
</body>

I put an attribute (similar to the id of the bean) to the application scope.
The output is ss ss pp
The second <jsp:setProperty> uses the same id.The <jsp:setProperty> in both cases have been executed. But HFSJ says that tag executes conditionally. According to that I think ss pp should not be printed, and the value set as "cc" has been overriden by "ss"

Please clarify this for me

thanks
shini
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the setProperty is not present as a body for the useBean it is executing unconditionally,If the <jsp:useBean>has <jsp:setProperty> in its body then it would executes conditionally.


Correct me if i am wrong.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The <jsp:setProperty> in both cases have been executed.

It will. What would prevent them from being executed ?

But HFSJ says that tag executes conditionally.

Could you clarify this statement ? What do they really say ?
If this has to do with setProperty being in the body of the useBean tag, then your example is not correct. setProperty is not in the body of useBean, so it will execute anytime.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean if the code have <jsp:useBean> <jsp:setProperty/> </jsp:useBean> then the set property is conditional .Refer page 350 HFSJ it will be more clear.
 
Shini Akshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HFSJ ,page350 there is highlighted part "with a <jsp:useBean> body, you can have code that runs conditionall, only if the bean attribute cant be found and a new bean is created".

In my code I set an attribute with the same instance name of the <jsp: useBean> tag id. Is this attribute cannot be considered as a bean Attribute?
If this <jsp:setProperty> is not to be run, what should be the code?

Thanks
Shini
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with a <jsp:useBean> body

Did the book tell you what a tag body was ? Do you see anything in the useBean body in your example ?
 
Shini Akshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:setProperty> of the first useBean tag can be considered as the body, isn't it.
Please correct me if I am wrong. Please let me know how the code should be modified ,if the setProperty tag not to be run conditionally.

Thanks
Shini
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The above answers already explained you what to do. Change your abc.jsp code
as follows :

abc.jsp
<body>
<jsp:useBean id="person" class="foo.Person" scope="application">
<jsp:setProperty name="person" property="name" value="ss" />
</jsp:useBean>

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

In this case the code execute only if the person attribute is not found.
See how the setProperty tag is nested in usebean tag for conditional setting.

Your Original example set the property unconditionally.
Thanks
[ November 21, 2007: Message edited by: Narendra Dhande ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you have to understand what the body of a tag is. With tags, you have an opening tag, a closing tag, and anything in between is its body. So for useBean, the body would be anything between <jsp:useBean> and </jsp:useBean>, such as what Narendra has posted. What is inside the useBean body is executed only if the tag instanciates the class. In your example, the object already exists in a scope, so there's no instanciation done by useBean. Therefore, what's inside its body would not be executed. But in your code above, the body is empty. All setProperty tags are outside, so they are all executed.
[ November 21, 2007: Message edited by: Christophe Verre ]
 
Shini Akshi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks so much all of you. I had the problem with starting and closing tags properly.Now I got it .

Thanks
Shini
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic