SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Simple Example for using JavaBeans in JSP

 Introduction

JavaBeans component design conventions govern the properties of the class, and the public methods that give access to the properties.

A JavaBeans component property can be:

  • Read/write, read-only, or write-only.
  • It means it contains a single value, or indexed, i.e. it represents an array of values.
There is no requirement that a property be implemented by an instance variable; the property must simply be accessible using public methods that conform to certain conventions:

  • For each readable property, the bean must have a method of the form:
    PropertyClass getProperty () { ... }
  • For each writable property, the bean must have a method of the form:
    setProperty (PropertyClass pc) { ... }
In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.

CLICK HERE to download this complete example (zip file)


 Store this JavaBean class inside the folder test (FindAuthor.java)

package test;
import java.io.Serializable;
import java.util.*;
import java.sql.*;
import java.io.*;
public class FindAuthor implements Serializable
{
public String url, authorName, driverName, authorId;
public Vector result;
public void setUrl(String url)
{
if(url!= null)
this.url = url;
}
public void setAuthorId(String authorId)
{
if (authorId != null)
this.authorId = authorId;
}
public void setDriverName(String driverName)
{
if (driverName != null)
this.driverName = driverName;
}
public String getAuthorId()
{
return(this.authorId);
}
public Vector getResult()
{
Vector v = new Vector();
try
{
Class.forName(driverName);
Connection con = DriverManager.getConnection(url, "sa", "");
PreparedStatement stat = con.prepareStatement( "SELECT * FROM Sample1 WHERE au_id = ? ");
stat.setString(1,authorId);
ResultSet rs = stat.executeQuery();
if(rs.next())
{
v.addElement(rs.getString("au_fname"));
v.addElement(rs.getString("address"));
v.addElement(rs.getString("city"));
v.addElement(rs.getString("state"));
v.addElement(rs.getString("zip"));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
this.result = v;
return result;
}
}
Download: FindAuthor.java

 GetAuthorName.jsp

<%@ page import ="java.util.*" %>
<%@ page import = "test.FindAuthor" %>
<html>
<head>
<title> JSP and JavaBean </title>
<font size=4 face="Verdana" color=#112244>
<jsp:useBean id="FA" scope="application" class="test.FindAuthor" />
<jsp:setProperty name="FA" property="*" />
</head>
<body>
<%
Vector v = (Vector)FA.getResult();
Enumeration enu = v.elements();
while(enu.hasMoreElements())
{
out.println("Author Name:"+enum.nextElement());
%>
<br>
<%
out.println("Address:"+enu.nextElement());
%>
<br>
<%
out.println("City:"+enu.nextElement());
%>
<br>
<%
out.println("State:"+enu.nextElement());
%>
<br>
<%
out.println("ZIP:"+enu.nextElement());
}
%>
</body>
</html>
Download: GetAuthorName.jsp

 WelcomePage.html

<html>
<body>
<form name="f1" action="GetAuthorName.jsp">
<br>
<h2><center>Connecting Database with JavaBean</center></h2>
<br>
<table cellspacing=8 cellpadding=8 bgcolor=GRAY colspan=2 rowspan=2 align="center" width="361" height="62">
<tr>
<td width="224" height="1"><b>
<font face="Verdana" color="#FFFFFF" size="3">
Author Id:
</font></b>
</td>
<td width="261" height="1">
<font size=4 face="Verdana" color=#008000>
<input type=text name="authorId" size="26" >
</font>
</td>
</tr>
<tr>
<td width="224" height="1"><b>
<font face="Verdana" color="#FFFFFF" size="3">
URL:
</font></b>
</td>
<td width="261" height="1">
<font size=4 face="Verdana" color=#008000>
<input type=text name="url" size="26" >
</font>
</td>
</tr>
<tr>
<td width="224" height="1"><b>
<font face="Verdana" color="#FFFFFF" size="3">
Driver Name:
</font></b>
</td>
<td width="261" height="1">
<font size=4 face="Verdana" color=#008000>
<input type=text name="driverName" size="26" >
</font>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Go">
</td>
</tr>
</table>
<br>
</form>
</body>
</html>
Download: WelcomePage.html


 Click Next To Continue ...
<- PREVIOUSNEXT ->