Hi All,
This is a small application it consists of a controller
servlet,one bean,three
jsp pages,web.xml.and it retrieves data from the data base.
controller:
package javaranch;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class EmployeeServlet extends HttpServlet {
private final static
String sql =
"select * from people_table where id = ?";
private Connection connection = null;
private PreparedStatement statement = null;
private ServletContext context;
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
String driver =context.getInitParameter("driver");
String url=context.getInitParameter("url");
try {
Class.forName(driver);
connection = DriverManager.getConnection(url);
statement = connection.prepareStatement(sql);
}
catch (ClassNotFoundException e) {
System.err.println("Unable to load database driver");
throw new ServletException("Unable to load database driver");
}
catch (SQLException e) {
System.err.println("Unable to connect to database");
throw new ServletException("Unable to connect to database");
}
}
public void service(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String jspPageUrl;
String cmd = req.getParameter("cmd");
String idString = req.getParameter("id");
int id;
try { id = Integer.parseInt(idString); }
catch(NumberFormatException e) { id=0; };
if ("get".equals(cmd)) {
EmployeeBean bean = fetchEmployee(id);
req.setAttribute("employee", bean);
jspPageUrl = "/arch/employee.jsp";
}
else {
Vector list = fetchAll();
req.setAttribute("list", list);
jspPageUrl = "/arch/list.jsp";
}
RequestDispatcher dispatcher;
dispatcher = context.getRequestDispatcher(jspPageUrl);
dispatcher.forward(req, res);
}
public EmployeeBean makeBean(ResultSet results)
throws SQLException {
EmployeeBean bean = new EmployeeBean(results.getInt("id"));
bean.setFirstName(results.getString("fname"));
bean.setLastName(results.getString("lname"));
bean.setEmail(results.getString("email"));
bean.setDepartment(results.getString("department"));
bean.setImage(results.getString("image"));
return bean;
}
public EmployeeBean fetchEmployee(int id) {
try {
ResultSet results;
synchronized (statement) {
statement.clearParameters();
statement.setInt(1, id);
results = statement.executeQuery();
}
EmployeeBean bean = null;
if (results.next()) {
bean = makeBean(results);
}
if (results != null)
results.close();
return bean;
}
catch (SQLException se) { return null; }
}
public Vector fetchAll() {
try {
Vector list = new Vector();
ResultSet results;
Statement st = connection.createStatement();
results = st.executeQuery("select * from people_table");
while (results.next())
list.add(makeBean(results));
return list;
}
catch (SQLException se) { return null; }
}
public void destroy() {
try {
if (connection != null)
connection.close();
}
catch (SQLException e) { }
}
}
------------------------------------------------
Bean:
package javaranch;
public class EmployeeBean {
private int id;
private String firstName;
private String lastName;
private String image;
private String email;
private String department;
public EmployeeBean(int id) {
this.id = id;
firstName = "";
lastName = "";
image = "";
email = "";
department = "";
}
public EmployeeBean() {
this(0);
}
public int getId() {
return this.id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setImage(String image) {
this.image = image;
}
public String getImage() {
return this.image;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return this.email;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDepartment() {
return this.department;
}
}
----------------------------------------------
employee.jsp
<%@ page import=" javaranch.EmployeeBean" %>
<jsp:useBean id="employee" class="javaranch.EmployeeBean"
scope="request" />
<html>
<head><title>employee record</title></head>
<body>
<table border="1" align="center">
<tr bgcolor="tan"><td colspan=2><font size=+3 face=arial><b>
<jsp:getProperty name="employee" property="lastName"/>,
<jsp:getProperty name="employee" property="firstName"/>
</b></font></td></tr>
<tr><td align=left valign=top><IMG height="150" SRC="<%= employee.getImage()%>"</td>
<td align=left valign=top>
<table border=0>
<tr><td><b>full name:</b></td><td>
<jsp:getProperty name="employee" property="firstName"/>
<jsp:getProperty name="employee" property="lastName"/>
</td></tr>
<tr><td><b>employee id:</b></td><td>
<jsp:getProperty name="employee" property="id"/>
</td></tr>
<tr><td><b>department:</b></td><td>
<jsp:getProperty name="employee" property="department"/>
</td></tr>
<tr><td><b>e-mail:</b></td><td>
<jsp:getProperty name="employee" property="email"/>
</td></tr>
</table>
</td>
</tr>
</table>
</body>
</html>
---------------------------------------------
list.jsp:
<%@ page import="java.util.* , javaranch.EmployeeBean" %>
<jsp:useBean id="employee" class="javaranch.EmployeeBean" />
<html>
<body>
<b>Current Employees</b>
<ul>
<%
Vector v = (Vector)request.getAttribute("list");
Iterator i= v.iterator();
while (i.hasNext()) {
employee = (EmployeeBean)i.next();
%>
<li>
<a href="/webAppPrefix/test?cmd=get&id=<%=String.valueOf(employee.getId()) %>
">
<%= employee.getFirstName() %>,
<%= employee.getLastName() %>,
</a>
<% } %>
</ul>
<HR>
<jsp:include page="included.jsp" flush="true" >
<jsp
aram name="webmaster" value="abc@hotmail.com" />
</jsp:include>
</body>
</html>
--------------------------------------------
included.jsp:
webmaster email:
<%=request.getParameter("webmaster") %>
------------------
web.xml
<web-app>
<context-param>
<param-name>driver</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>
jdbc dbc:emp</param-value>
</context-param>
<servlet>
<servlet-name>fetch</servlet-name>
<servlet-class>javaranch.EmployeeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fetch</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
I HAVE USED ACCESS DATA BASE
DSN NAME--emp
data base name---any-
dsn name matters--only
table name---people_table
ID--NUMBER
FNAME--TEXT
LNAME--TEXT
DEPARTMENT---TEXT
EMAIL--TEXT
IMAGE---TEXT-----consists the url's for the images
/webAppPrefix/sample.gif
Add some records in the people_table
dir structure:
webAppPreFix/WEB-INF/web.xml
webAppPreFix/WEB-INF/classes/javaranch/both the classes here(servlet/bean)
webAppPreFix/arch/all jsp pages here.
webAppPreFix/yourImage
type in your browser:
..../webAppPrefix/test
or
...../webAppPrefix/test?cmd=get&id=1
Clients should not be able access the jsp pages directly,to implement this
(security)
click here Its a very simple application but we can add different functionalities to improve it.
(update,add,delete)
SETTING
TOMCAT (WINDOWS 2000)
my jdk1.3 is installation directory d:\jdk1.3
tomcat installation directory is ---tomcat d:\tomcat
>controlpanel>system>Advanced>Environment Variables>system variables>New---type CLASSPATH in the upper textfield
and the value in the lower textfield
.;d:\tomcat\common\lib\servlet.jar
HERE tomcat is the name of tomcat installation directory.
ok
next set the path:
PATH ----upper textfield
d:\jdk1.3\bin ----value in lower tf.
next set catalina_home:
CATALINA_HOME ----upper tf
d:\tomcat ---value lower tf.
HERE tomcat is the name of tomcat installation directory.
set java_home:
JAVA_HOME ----upper tf
d:\jdk1.3 ----lower tf
OR/and
Edit setclasspath.batch file located at tomcat>bin>setclasspath.batch:
here set java_home like this:
set JAVA_HOME=d:\jdk1.3
This batch file looks like this:
rem ---------------------------------------------------------------------------
rem Set CLASSPATH and
Java options
rem
rem $Id: setclasspath.bat,v 1.8 2003/01/17 10:07:21 remm Exp $
rem ---------------------------------------------------------------------------
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJavaHome
set JAVA_HOME=d:\jdk1.3 <---------------------------------------------HERE
:gotJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
goto okJavaHome
-------------------------------------------------------
To confirm your settings
type:
http://localhost:8080/ Enter
u should get the default tomcat home page.
I hope it helps!
[ August 05, 2003: Message edited by: Amer Khan ]