JSP tutorial : JavaBeans

JSP tutorial : JavaBeans


JSP (JavaServer Pages)

JavaBeans

JavaBeans is a special Java class that has specific methods to be called to work in Jsp pages. It is commonly used to handle data input by the user in html form. The data input in the form will be stored in the beans. Therefore, the data can be used later in other jsp pages. The most important thing about JavaBeans is the code written in the beans can be reusable.

Example: In this example, we will ask the user to input his/her name and e-mail on DataCollection.jsp page. Once the use clicks on submit button, the data will be stored in a bean called UserInfo class located in WEB-INF/classes/userpack folder and then these information will be processed in a Thank.jsp.

 

UserInfo.java (WEB-INF/classes/userpack/UserInfo.java)

package userpack;

public class UserInfo{

private String youremail;

private String yourname;

public UserInfo() {

yourname = null;

youremail=null;

}

public void setYourname( String name ) {

yourname = name;

}

public String getYourname() {

return yourname;

}

 

public void setYouremail( String email ) {

youremail = email;

}

public String getYouremail() {

return youremail;

}

 

}

 

Remind: Don’t forget to compile UserInfo.java to UserInfo.class file.

 

DataCollection.jsp

 

<%@ page import="userpack.UserInfo" %>

<jsp:useBean id="beanName" scope="page" class=" userpack.UserInfo " />

<jsp:setProperty name="beanName" property="yourname" />

<jsp:useBean id="beanEmail" scope="page" class=" userpack.UserInfo " />

<jsp:setProperty name="beanEmail" property="youremail" />

 

<html>

<head><title>User Data Collection</title></head>

<body bgcolor="#FF00C0" >

 

<table border="0" width="600">

<tr>

<td width="140"> &nbsp; </td>

<td width="540">

<h1>Please enter your name and e-mail</h1>

</td>

</tr>

<tr>

<td width="140" &nbsp; </td>

<td width="540">

<form method="get">

<table>

<tr><td>Name:</td>

<td><input type="text" name="yourname" size="20"></td>

</tr>

<tr> <td>E-mail:</td>

<td><input type="text" name="youremail" size="20"></td>

</tr>

</table>

<br>

<input type="submit" value="Submit">

<input type="reset" value="Reset">

</td>

</tr>

</form>

</table>

<%

if ( request.getParameter("yourname") != null ) {

%>

<%@ include file="Thank.jsp" %>

<%

}

%>

</body>

</html>

 

Thank.jsp

<html>

<body>

<table border="0" width="600">

<tr>

<td width="140">

&nbsp;

</td>

<td width="540">

<h1>Thank, <jsp:getProperty name="beanName" property="yourname" />! <br />

Your email is:<jsp:getProperty name="beanEmail" property="youremail" />

</h1>

</td>

</tr>

</table>

</body>

</html>

Note: request.getParameter() is used to get information submitted by post or get method.method will hide information in address bar and it does not limit the length of information that is attached to the address. The method will display information attached with the address in address bar. It limits to the short length of information (100 characters).

jsp beans  




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.