Below is a simple HTML Link manager that I wrote when I was learning
servlets.  If a GET request comes in it displays the links it has collected.
If a POST request comes in, it expects to receive the information to add
another link to it's list of remembered links.  Pretty simplistic, but
different behavior between the get and post sides.  It's not the most
efficient beast, but it's a pretty good example program.
    (*Chris*)

/**
 * Links - Servlet to maintain a list of links
 *
 * @author Chris Pratt
 * @version 1.0
 *
 * Properties
 *   dbDriver oracle.jdbc.driver.OracleDriver
 *   database jdbc:oracle:thin:@localhost:1521:ORCL
 *   user     User
 *   pwd      Password
 *   table    science
 *   title    Fun with Science!
 *
 * 8/24/1998
 */
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Links extends HttpServlet {
  protected String conString;
  protected String conUser;
  protected String conPwd;
  protected String table;
  protected String title;

  public void init (ServletConfig cfg) throws ServletException {
    super.init(cfg);
    try {
      Class.forName(cfg.getInitParameter("dbDriver"));
    } catch(ClassNotFoundException cx) {
      throw new ServletException("Can't load Database Driver [" +
cx.getMessage() + "]");
    }
    conString = cfg.getInitParameter("database");
    conUser = cfg.getInitParameter("user");
    conPwd = cfg.getInitParameter("pwd");
    table = cfg.getInitParameter("table");
    title = cfg.getInitParameter("title");
  } file://init

  public void doGet (HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException {
    try {
      Connection c = DriverManager.getConnection(conString,conUser,conPwd);
      Statement s = c.createStatement();
      Object[] args = {table};
      String query = MessageFormat.format("SELECT * FROM {0}",args);
      ResultSet rs = s.executeQuery(query);
      ServletOutputStream out = res.getOutputStream();
        // set content type and other response header fields first
      res.setContentType("text/html");
        // then write the data of the response
      out.println("<HTML>");
      out.println("  <HEAD>");
      if(title != null) {
        out.println("    <TITLE>" + title + "</TITLE>");
      } else {
        out.println("    <TITLE>Live Links!</TITLE>");
      }
      out.println("  </HEAD>");
      out.println("  <BODY>");
      if(title != null) {
        out.println("    <H1>" + title + "</H1>");
        out.println("    <HR><BR>");
      }
      while(rs.next()) {
        out.println("    <A HREF=" + rs.getString("ADDRESS") + ">" +
rs.getString("DESCRIPTION") + "</A><BR>");
      }
      out.println("    <HR><BR>");
      out.println("    <P>Add your own link");
      out.println("    <FORM ACTION=\"/servlet/LiveLinks\"
METHOD=\"Post\">");
      out.println("      <P>Description <INPUT TYPE=\"Text\"
NAME=\"Description\"><BR>");
      out.println("      <P>Address <INPUT TYPE=\"Text\"
NAME=\"Address\"><BR>");
      out.println("      <P><INPUT TYPE=\"Submit\" VALUE=\"Submit\">");
      out.println("      <INPUT TYPE=\"Reset\" VALUE=\"Clear\"><BR>");
      out.println("    </FORM>");
      out.println("    <ADDRESS><A
HREF=\"mailto:[EMAIL PROTECTED]\">Chris Pratt:
WebMaster</A></ADDRESS>");
      out.println("  </BODY>");
      out.println("</HTML>");
      out.close();
      c.close();
    } catch(SQLException sx) {
      ServletOutputStream err = res.getOutputStream();
      res.setContentType("text/html");
      err.println("<HTML>\n  <HEAD>\n    <TITLE>Live Links - SQL
Exception</TITLE>\n  </HEAD>\n  <BODY>");
      err.println("    <P>An SQL Exception Occurred while processing the
request [" + sx.getMessage() + "]");
      err.println("    <UL>");
      err.println("     <LI>conString = " + conString);
      err.println("     <LI>conUser = " + conUser);
      err.println("     <LI>conPwd = " + conPwd);
      err.println("     <LI>table = " + table);
      err.println("    </UL>");
      err.println("  </BODY>\n</HTML>");
      err.close();
    }
  } file://doget

  public void doPost (HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException {
    String query = null;
    try {
      ServletOutputStream out = res.getOutputStream();
      res.setContentType("text/html");
      Connection c = DriverManager.getConnection(conString,conUser,conPwd);
      Statement s = c.createStatement();
      Object[] args =
{table,req.getParameterValues("Description")[0],req.getParameterValues("Addr
ess")[0]};
      query = MessageFormat.format("INSERT INTO {0} VALUES(''{2}'',
''{1}'')",args);
      out.println("<HTML>");
      out.println("  <HEAD>");
      if(s.executeUpdate(query) > 0) {
        out.println("    <TITLE>Update Accepted</TITLE>");
        out.println("  </HEAD>");
        out.println("  <BODY>");
        out.println("    <P>Thank you for contributing to the list.");
        out.println("    <P><A HREF=\"/servlet/LiveLinks\">Return to the
list</A>");
      } else {
        out.println("    <TITLE>Update Failed</TITLE>");
        out.println("  </HEAD>");
        out.println("  <BODY>");
        out.println("    <P>I'm sorry, for some reason your update did not
work, please contact the webmaster.");
        out.println("    <P><A HREF=\"/servlet/LiveLinks\">Return to the
list</A>");
      }
      out.println("    <ADDRESS><A
HREF=\"mailto:[EMAIL PROTECTED]\">Chris Pratt:
WebMaster</A></ADDRESS>");
      out.println("  </BODY>");
      out.println("</HTML>");
      out.close();
      c.close();
    } catch(SQLException sx) {
      ServletOutputStream err = res.getOutputStream();
      res.setContentType("text/html");
      err.println("<HTML>\n  <HEAD>\n    <TITLE>Live Links - SQL
Exception</TITLE>\n  </HEAD>\n  <BODY>");
      err.println("    <P>An SQL Exception Occurred while processing the
request [" + sx.getMessage() + "]");
      err.println("    <UL>");
      err.println("     <LI>conString = " + conString);
      err.println("     <LI>conUser = " + conUser);
      err.println("     <LI>conPwd = " + conPwd);
      err.println("     <LI>table = " + table);
      err.println("     <LI>query = " + query);
      err.println("    </UL>");
      err.println("  </BODY>\n</HTML>");
      err.close();
    }
  }  file://doPost

  public String getServletInfo () {
    return "Live Link Manager";
  } file://getServletInfo

} file://*Links

----- Original Message -----
From: ARCHAIMBAULT SYLVAIN SOPRA <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, September 17, 1999 1:01 AM
Subject: Re: Stil having Problem with POST FORM


> --- Reçu de       RENEXTER.ARCHAISY ARCHAIMBAULT * 17/09/99 10.01
>
>
>  For my part, I make the same thing as Steven, because I really
> don't see why you will want different behaviour with GET and POST
> method. Can you give us an example of a situation in which you
> want to do that.
>
>                           sly
>
> From: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> Date: Thu, 16 Sep 1999 11:43:28 -0500
> Subject: Re: Still having Problem with POST FORM
>
> On Thu, 16 Sep 1999, Steven J. Owens wrote:
>
> [ ... ]
> >      Frankly, I override the service() method.  Various people
> have
> > said "this is bad" but I've never actually heard a coherent,
> cogent
> > explanation of why it's bad.  99% of the time you don't really
> care
> > whether they use GET or POST.  The main reason not to use GET is
> for
> [ ... ]
>
> Or perhaps you just didn't appreciate those explanations.  What
> the
> default service() does is very nice, farming requests out to
> appropriate doXXX() methods.  So when you want to do different
> things
> with GET and POST (or whatever) HTTP methods, it's very nice.  And
> still it's very easy to handle the case when they do the same
> thing.
>
> Perhaps you've only worked with servlets that do the same thing
> with
> GET and POST.  But that doesn't mean that 99% of applications work
> that way.  And if you ever have to change, it'll be a lot easier
> if
> you had just left service() alone in the first place.
>
> In addition, not having heard a compelling argument for not
> overriding
> service() is *not* a compelling argument *for* overriding
> service().
>
> Milt Epstein
> Research Programmer
> Software/Systems Development Group
> Computing and Communications Services Office (CCSO)
> University of Illinois at Urbana-Champaign (UIUC)
> [EMAIL PROTECTED]
>
> ---- 17/09/99 10.01 ---- Envoyé à      ---------------------------
>   -> servlet-interest(a)java.sun.com
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to