wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes getProperty question 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 "getProperty question" Watch "getProperty question" New topic
Author

getProperty question

H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
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

Joined: May 11, 2004
Posts: 298
<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>


Kapil Munjal
SCJP 1.4, SCWCD 1.4
H Singh
Ranch Hand

Joined: Apr 03, 2005
Posts: 34
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

Joined: May 11, 2004
Posts: 298
<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

Joined: Apr 03, 2005
Posts: 34
Thanks a lot.. Kapil
 
 
subject: getProperty question
 
Similar Threads
EL Expressions
JSP - cannot resolve symbol
JavaBean from a servlet - still not resolved
Getting wrong result ,but why?
Why tomcat act like it error?