Reputation: 497
I want to invoke a getter method (returns String value) of a Java class from JSP by using "jsp:usebean", but it returns a null value. What I don't understand is why it can't return the updated value.
Can someone shed some light on this?
Should I use a Cookie
to get the value from JSP?
Upvotes: 3
Views: 8588
Reputation: 696
It is simple.
In java file:
package loga;
class bean{
String name;
public void setName(String Uname)
{
this.name=Uname;
}
public void getName()
{
return name;
}
In jsp file, call this method as:
<jsp:useBean id="object" class="loga.bean">
<jsp:setproperty name="object" property="Name" Value="XXXX"/>
<jsp:getProperty name="object" property="Name"/>
</jsp:usebean>
Here, the property indicates the method name of the getName() in the java class. To pass value from other controls use param property and give name of the control.
Upvotes: 3
Reputation: 7651
I'm not sure what you're using (Struts, plain Servlets, etc.) but essentially you need to add an attribute to the ServletRequest like:
class Person {
private String firstName;
// other fields, getters, setters
}
public void method(HttpServletRequest httpServletRequest) {
Person p = new Person();
p.setFirstName("Obama");
httpServletRequest.setAttribute("person", p);
}
and in your JSP:
<jsp:getProperty object="person" property="firstName" />
or if you use JSTL:
<c:out value="${person.firstName}"/>
Upvotes: 5