FileDocCategorySizeDatePackage
Guestbook.javaAPI DocExample5097Thu Apr 05 01:54:12 BST 2001None

Guestbook

public class Guestbook extends com.oreilly.servlet.CacheHttpServlet

Fields Summary
static final String
SELECT_ALL
static final String
INSERT
private long
lastModified
private ConnectionPool
pool
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    printHeader(out);
    printForm(out);
    printMessages(out);
    printFooter(out);
  
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    handleForm(req, res);
    doGet(req, res);
  
public longgetLastModified(javax.servlet.http.HttpServletRequest req)

    return lastModified;  // supports CacheHttpServlet
  
private voidhandleForm(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    String name = req.getParameter("name");
    String email = req.getParameter("email");
    String comment = req.getParameter("comment");

    Connection con = null;
    PreparedStatement pstmt = null;
    try {
      con = pool.getConnection();
      // Use a prepared statement for automatic string escaping
      pstmt = con.prepareStatement(INSERT);
      long time = System.currentTimeMillis();
      pstmt.setString(1, Long.toString(time));
      pstmt.setString(2, name);
      pstmt.setString(3, email);
      pstmt.setString(4, comment);
      pstmt.executeUpdate();
    }
    catch (SQLException e) {
      throw new ServletException(e);
    }
    finally {
      try {
        if (pstmt != null) pstmt.close();
      }
      catch (SQLException ignored) { }
      pool.returnConnection(con);
    }

    // Make note we have a new last modified time
    lastModified = System.currentTimeMillis();
  
public voidinit()

  
  // Get a pointer to a connection pool
       
    try {
      ServletContext context = getServletContext();
      synchronized (context) {
        // A pool may already be saved as a context attribute
        pool = (ConnectionPool) context.getAttribute("pool");
        if (pool == null) {
          // Construct a pool using our context init parameters
          // connection.driver, connection.url, user, password, etc
          pool = new ConnectionPool(new ContextProperties(context), 3);
          context.setAttribute("pool", pool);
        }
      }
    }
    catch (Exception e) {
      throw new UnavailableException(
      "Failed to fetch a connection pool from the context: " + e.getMessage());
    }
  
private voidprintFooter(java.io.PrintWriter out)

    out.println("</BODY>");
  
private voidprintForm(java.io.PrintWriter out)

    out.println("<FORM METHOD=POST>");  // posts to itself
    out.println("<B>Please submit your feedback:</B><BR>");
    out.println("Your name: <INPUT TYPE=TEXT NAME=name><BR>");
    out.println("Your email: <INPUT TYPE=TEXT NAME=email><BR>");
    out.println("Comment: <INPUT TYPE=TEXT SIZE=50 NAME=comment><BR>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\"Send Feedback\"><BR>");
    out.println("</FORM>");
    out.println("<HR>");
  
private voidprintHeader(java.io.PrintWriter out)

    out.println("<HTML><HEAD><TITLE>Guestbook</TITLE></HEAD>");
    out.println("<BODY>");
  
private voidprintMessages(java.io.PrintWriter out)

    String name, email, comment;

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
      con = pool.getConnection();
      stmt = con.createStatement();
      rs = stmt.executeQuery(SELECT_ALL);

      while (rs.next()) {
        name = rs.getString(1);
        if (rs.wasNull() || name.length() == 0) name = "Unknown user";
        email = rs.getString(2);
        if (rs.wasNull() || email.length() == 0) name = "Unknown email";
        comment = rs.getString(3);
        if (rs.wasNull() || comment.length() == 0) name = "No comment";
        out.println("<DL>");
        out.println("<DT><B>" + name + "</B> (" + email + ") says");
        out.println("<DD><PRE>" + comment + "</PRE>");
        out.println("</DL>");
      }
    }
    catch (SQLException e) {
      throw new ServletException(e);
    }
    finally {
      try {
        if (stmt != null) stmt.close();
      }
      catch (SQLException ignored) { }
      pool.returnConnection(con);
    }