In this JSP Tutorial, we will see how to use Java Bean in a JSP file. Here, you will learn the Tags:- jsp:useBean, jsp:setProperty and jsp:getProperty.
Customer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package tube.codingexamples.bean; //Sample 01: Customer Bean //Make sure the fields follow Java Std. //First letter smaller case. public class Customer { //1.1: Create Fields for Customer (Note: Private) private String custID = ""; private String custName = ""; private String locationZip = ""; //1.2: Bean Setter and Getter public String getCustID() { return custID; } public void setCustID(String custID) { this.custID = custID; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getLocationZip() { return locationZip; } public void setLocationZip(String locationZip) { this.locationZip = locationZip; } } |
CustomerDetail.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- Sample 02: Create Form Fields Matching with Bean Fields --> <form action="RespCustDetail.jsp"> Customer ID: <input type="text" name="custID" size="4"><br> Customer Name: <input type="text" name="custName" size="25"><br> ZIP: <input type="text" name="locationZip" size="10"><br> <input type="submit" name="submit" value="Register"> </form> </body> </html> |
RespCustDetail.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- Sample 03: Access Java Bean Fields --> <jsp:useBean id="Customer" class="tube.codingexamples.bean.Customer"> <jsp:setProperty name="Customer" property="*" /> </jsp:useBean> <H1> Customer Details </H1> <p> Customer <jsp:getProperty property="custName" name="Customer"/> identified by <b><jsp:getProperty property="custID" name="Customer"/></b> is living in the location <jsp:getProperty property="locationZip" name="Customer"/> </body> </html> |
Categories: JSP