Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getProperty question

 
H Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use getProperty to print both name and value. Please take a look at the code below :-

Person.java:

public class Person{

private String name;
private int value;

public void setName(String name){
this.name = name;
}

public String getName(){
return name;
}

public void setValue(int value){
this.value = value;
}

public int getValue(){
return value;
}
}

PersonServlet.java:

public class PersonServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

Person p = new Person();

p.setName("Evan");
p.setValue(1);

request.setAttribute("person",p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}

result.jsp:

<html><body>
<jsp:useBean id="person" class="com.example.Person" scope="request" />
Person created by: <jsp:getProperty name="person" property="name" />
</body></html>

What will it take to print Evan and 1 ? Thanks
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html><body>
<jsp:useBean id="person" class="com.example.Person" scope="request" />
Person created by: <jsp:getProperty name="person" property="name" />

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

</body></html>
 
H Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kapil...dont know what i was thinking when i posted the question...

one more question ....

i am trying to use setProperty , below is my jsp :

result.jsp:

<html><body>

<jsp:useBean id="person" class="com.example.Person" scope="page" >
Person created by: <jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>

</body></html>

and PersonServlet.java

public class PersonServlet extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{

Person p = new Person();

//p.setName("Evan");
//request.setAttribute("person",p);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);
}
}

It says "the property value will be set only if a new bean is created. If an existing bean with that scope and id are found, the body of the tag will never run, so property won't be reset from your JSP code "

So i commented

//p.setName("Evan");
//request.setAttribute("person",p);

still i do not get Fred in the output. Thanks
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<jsp:useBean id="person" class="com.example.Person" scope="page" >
Person created by: <jsp:setProperty name="person" property="name" value="Fred" />
</jsp:useBean>


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

Try this!!!
 
H Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.. Kapil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic