[an error occurred while processing this directive]

Setting and getting JavaBean properties in JSP

                         

This example shows how to set and get properties of java bean class in jsp page. The important rules of setting and getting of properties are following:

  1. The set method name and property name must be same but with first letter in upper case and prefix with 'set'.
  2. The return type of the set method should be void and should take only one input parameter.
  3. The get method name and property name must be same but with first letter in upper case and prefix with 'get'.
  4. The return type of the get method can be String or any primitive type and should take no input parameter.

Source code of the JSPBean.java

package roseindia;
import java.io.*;

public class JSPBean {

  private String developer = "Sandeep";
  private String company = "Roseindia";
  private String city = "Delhi";

  public void setDeveloper(String developer) {
    developer = developer;
  }
  public String getDeveloper() {
    return developer;
  }

  public void setCompany(String company) {
    company = company;
  }
  public String getCompany() {
    return company;
  }
    
  public void setCity(String city) {
    city = city;
  }
  public String getCity() {
    return city;
  }  
}

useBean.jsp

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">

<html>
        <body>
                <jsp:useBean id="b" class="roseindia.JSPBean"/>

                <jsp:setProperty name="b" property="developer" />
                Developer = 
                <jsp:getProperty name="b" property="developer"/><br/>

                <jsp:setProperty name="b" property="company" />
                Company = 
                <jsp:getProperty name="b" property="company"/><br/>

                <jsp:setProperty name="b" property="city" />
                City = 
                <jsp:getProperty name="b" property="city"/><br/>

        </body>
</html>

</jsp:root>

Compile this program on the command prompt: 

C:\apache-tomcat-6.0.16\webapps\ServletExample\WEB-INF\classes>javac -d . JSPBean.java

Start the tomcat and run the jsp file on browser as given url: http://localhost:8080/ServletExample/jsp/useBean.jsp  then the output will be displayed as below:

Download Source Code

                         

[an error occurred while processing this directive]