|
IMPLEMENT JAVABEANS IN JSP
Problem Statement
Create a jsp application that connects to database and retrieves the details, such as author id, address, city, state and zip code related to authors. The javabean component should accept the authored, driver name, and URL as a parameter. The information is to be retrieved from author’s table.
Solution
• Create the javabeans
• Create jsp page
• Create the user interface using html
• Access the jsp application
Create javabeans
/*import the packages*/
package test;
import java.io.Serializable;
import java.util.*;
import java.sql.*;
import java.io.*;
/*Creating an class FindAuthor and implement the Serializable interface*/
public class FindAuthor implements Serializable
{
public String url, authorName, driverName, authorId;
public Vector result;
/*Setting the url property*/
public void setUrl(String url)
{
if(url !=null)
this.url=url;
}
/*Setting the authorname property*/
public void setAuthorId(String authorId)
{
if(authorId !=null)
this.authorId=authorId;
}
/*Setting the driverName property*/
public void setDriverName(String driverName)
{
if(driverName !=null)
this.driverName=driverName;
}
public String getAuthorId()
{
return(this.authorId);
}
/*Defining a method to fetch the result from author database*/
public Vector getResult()
{
Vector v=new Vector();
try
{
/*Load the driver*/
Class.forName(driverName);
/*Creating a Connection*/
Connection con=DriverManager.getConnection(url, "java", "javaclass");
/*Creating preparedStatement*/
PreparedStatement pstmt=con.prepareStatement("select * from authors where au_id=?");
/* set the authors name in the query*/
pstmt.setString(1, authorId);
/*Get the result set from the author*/
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
/*Storing the resultset in the vector v */
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"));
}
}
/*Handling the exception*/
catch(Exception ex)
{
ex.printStackTrace();
}
/*Returing the vector*/
this.result=v;
return result;
}
}
Create Jsp Code
<%--import the package--%>
<%@ page import ="java.util.*"%>
<%@ page import ="test.FindAuthor"%>
<html>
<head>
<title>JSP and JavaBean</title>
<font size=4 face="Verdana" color=yellow>
<%--Create an instance of FindAuthor class-%>
<jsp:useBean id="FindAuthor" scope="application" class="test.FindAuthor" />
<% set the value of various attribute, such as authorId, url, and driverName--%>
<jsp:setProperty name="FindAuthor" property="*"/>
<body>
<%--The details about the authors, <jsp:getProperty name="FindAuthor" property="authorName"/> is:<br>--%>
<%
/* Create an instance of type of vector and invoking the function getResult() to return the result set*/
Vector v=(Vector)FindAuthor.getResult();
Enumeration enum=v.elements();
/*prints the elements in the vector variable */
while(enum.hasMoreElements())
{
out.println("Author Name:"+enum.nextElement());
%>
<br>
<%
out.println("Address: "+enum.nextElement());
%>
<br>
<%
out.println("City: "+enum.nextElement());
%>
<br>
<%
out.println("State: "+enum.nextElement());
%>
<br>
<%
out.println("Zip: "+enum.nextElement());
}
%>
</body>
</html>
Code for user interface
<html>
<body>
<form name="f1" action="/forum/GetAuthorName.html">
<font size=2 face="verdana" color=blue>
<h2><center>Connecting to Database with JavaBean</center></h2>
<table cellspacing=5 cellpadding=5 bgcolor=gray colspan=3 rowspan=3 align="center" width="360" height="60">
<tr>
<td>Author Id:</td>
<td><input type=text name="id"></td>
</tr>
<br>
<tr>
<td>DriverName:</td>
<td><input type=text name="driver"></td>
</tr>
<br>
<tr>
<td>URL:</td>
<td><input type=text name="url"></td>
</tr>
</table>
<table align="center">
<tr>
<td><input type="submit" value="Go"></td>
</tr></table><br>
</font>
</form>
</body>
</html>
ERRORS ENCOUNTER THEY ARE:
1. Command Prompt:
Note:FindAuthor.java uses unchecked or unsafe operations.
Note:Recompile with –Xiint:unchecked for details.
2. DeployTool(New Web Application Wizard)
Error Dialog: No servlet class was added to war. Please go back to add the needed class file. Even when I added servlet class
Please what would I do? Thanks for the Respond
snookcent@yahoo.com, iykellyelove@yahoo.co.uk
|
|
|
|
|
|
|
// |