Menus

Jun 6, 2016

Write a simple java bean and jsp program in Netbeans

The following example shows how to create a bean:
Step 1: Create a jsp named as index.jsp inside Web Pages:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="first.jsp">
Firstname<input type="text" name="fname"><br>
Lastname<input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>


Step 2: Create a jsp named as first.jsp inside Web Pages:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="stud" scope="request" class="bean.BeanFinder"/>
<jsp:setProperty name="stud" property="*"/>
<jsp:forward page="display.jsp"/>
</body>
</html>

 Step 3: Create a package named bean and make a java class named BeanFinder.java
package bean;
/**
*
* @author Paawan
*/
public class BeanFinder {
private String fname;
private String lname;
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
}

 Step 4: Once the java class is created, create display.jsp inside Web Pages:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="stud" scope="request" class="bean.BeanFinder"/>
Fname is <jsp:getProperty name="stud" property="fname"/>
Lname is <jsp:getProperty name="stud" property="lname"/>
</body>

</html>

No comments:

Post a Comment

Contact Form

Name

Email *

Message *