Home Jsp Setting and getting JavaBean properties in JSP
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Setting and getting JavaBean properties in JSP

Advertisement
This example shows how to set and get properties of java bean class in jsp page.

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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 1, 2008

Ask Questions?    Discuss: Setting and getting JavaBean properties in JSP  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com