Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

getProperty question

 
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
 
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic