Has anyone worked through the examples in the book by Duane Fields entitled,
"Web Development with Java Server Pages"?  I am working through the example
in chapter 8 and I have run into a problem.  This example uses a servlet to
access a database and send the information along to either 'list.jsp' or
'employee.jsp' depending on whether the servlet received a querystring.  If
it doesn't receive an employee ID in the query string it retrieves all
records, puts them in a Vector and sends them to 'list.jsp' whose job is to
display all records.  If it receives an employee ID it retrieves that record
from the database and sends it to 'employee.jsp' for display.

The application runs when I send the servlet and employee ID.  However, when
I access the servlet to request all records (3 records in all), the servlet
returns 3 things in the vector but they don't display.  They don't seem to
map properly to the employee been.  All employee IDs are 0 and all names are
equal to "", which is how the bean initializes.

I have attached the scripts if anyone is interested.  I am using Tomcat.
The strange part is when I run it in JDeveloper it works, but it doesn't
work in Tomcat.

Kevin


Four scripts follow:
        - List.jsp
        - Employee.jsp
        - FetchEmployeeServelet.java
        - EmployeeBean.java


LIST.JSP
--------------------------------------------
<%@ page import="java.util.*" %>
<jsp:useBean id="employee" class="jspbook.ch8.EmployeeBean" scope="request"
/>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=WINDOWS-1252">
<META NAME="GENERATOR" CONTENT="Oracle JDeveloper">
<TITLE>Employee List Page</TITLE>
</HEAD>
<BODY>
<b>Current Employee</b>
<ul>
<%
  Vector v = (Vector)request.getAttribute("list");
  Iterator i = v.iterator();
  while (i.hasNext()) {
    employee = (jspbook.ch8.EmployeeBean)i.next();
%>
  <li>
  <a href="servlet/FetchEmployeeServlet?cmd=get&id=<jsp:getProperty
name="employee" property="id"/>">
  <jsp:getProperty name="employee" property="lastName"/>
  <jsp:getProperty name="employee" property="firstName"/>
<%
}
%>
</ul>
</BODY>
</HTML>


EMPLOYEE.JSP
--------------------------------------------
<%@ page contentType="text/html;charset=WINDOWS-1252"%>
<%@ page import="jspbook.ch8.EmployeeBean" %>
<jsp:useBean id="employee" class="jspbook.ch8.EmployeeBean" scope="request"
/><HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=WINDOWS-1252">
<META NAME="GENERATOR" CONTENT="Oracle JDeveloper">
<TITLE>Employy Record</TITLE>
</HEAD>
<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 src="<jsp:getProperty name="employee" property="image" />"></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>Email:</b></td>
      <td>
       <jsp:getProperty name="employee" property="email" /></td></tr>
    </table>
  </td></tr>
</table>
<BODY>
</BODY>
</HTML>


FetchEmployeeServlet.java
--------------------------------------------
// package jspbook.ch8; //  Note running it from web-inf/classes

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import jspbook.ch8.*;

// import oracle.jdeveloper.servlet.*;
// import oracle.jbo.*;

public class FetchEmployeeServlet extends HttpServlet {
  private final static String driver = "oracle.jdbc.driver.OracleDriver";
  private final static String url =
"jdbc:oracle:thin:@localhost:1521:DBORAC2";
  private final static String user = "KEVIN";
  private final static String password = "KEVIN123";
  private final static String sql = "SELECT * from PEOPLE_TABLE WHERE
EMP_ID=?";
  private Connection connection = null;
  private PreparedStatement statement = null;
  private ServletContext context;

  /**
   * Initialize global variables
   */
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    context = config.getServletContext();
    try {
      Class.forName(driver);
      connection = DriverManager.getConnection(url, user, password);
      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!");
    }
  }

  /**
   * Service the request
   */
  public void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
    String jsp;
    String cmd = request.getParameter("cmd");
    String idString = request.getParameter("id");
    int id;
    try {
      id = Integer.parseInt(idString);
    }
    catch (NumberFormatException e) {
      id = 0;
    }

    if ("get".equals(cmd)) {
      EmployeeBean bean = fetchEmployee(id);
      request.setAttribute("employee", bean);
      jsp = "/jsp/jspbook/ch8/employee.jsp";
    }
    else {
      Vector list = fetchAll();
      request.setAttribute("list", list);
      jsp = "/jsp/jspbook/ch8/list.jsp";
    }
    RequestDispatcher dispatcher;
    // dispatcher = context.getRequestDispatcher(jsp);
    dispatcher = getServletContext().getRequestDispatcher(jsp);
    dispatcher.forward(request, response);
  }

  /**
   * Create Employee Bean and set to user whose ID is ID
   * @return EmployeeBean
   */
  public EmployeeBean makeBean(ResultSet results) throws SQLException {
    EmployeeBean bean = new EmployeeBean(results.getInt("emp_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;
  }

  /**
   * Create Employee Bean and set to user whose ID is ID
   * @return EmployeeBean
   */
  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;
    }
  }

  /**
   * Create Employee Bean and set to user whose ID is ID
   * @return EmployeeBean
   */
  public Vector fetchAll() {
    try {
      Vector list = new Vector();
      ResultSet results;
      EmployeeBean bn;
      Statement st = connection.createStatement();
      results = st.executeQuery("SELECT * FROM PEOPLE_TABLE");
      while (results.next())

        bn = makeBean(results);
        list.add(bn);
      }
      return list;
    }
    catch (SQLException se) {
      return null;
    }
  }

  public void destroy() {
    try {
      if (connection != null) connection.close();
    }
    catch (SQLException e) {}
  }

  /**
   * Get Servlet information
   * @return java.lang.String
   */
  public String getServletInfo() {
    return "jspbook.ch8.FetchEmployeeServlet Information";
  }
}



EmployeeBean.java
--------------------------------------------
package jspbook.ch8;

public class EmployeeBean extends Object {
  private int id;
  private String firstName;
  private String lastName;
  private String image;
  private String email;
  private String department;

  /**
   * Constructor
   */
  public EmployeeBean() {
    this(0);
  }

  public EmployeeBean(int id) {
    this.id = id;
    firstName = "";
    lastName = "";
    image = "";
    email = "";
    department = "";
  }

  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;
  }

}

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to