/** File name: FormHandler.java * * Author: Quansheng Xiao, Jeff Offutt * * Date: 9-11-2001 */ // Import Servlet Libraries import javax.servlet.*; import javax.servlet.http.*; // Import Java Libraries import java.io.*; import java.util.*; import java.lang.*; // FormHandler class // Generic form handler -- feed back all the parameters and values // that a client inputs from an HTML form. // Note: the "name" of the submit button in the form must be "submit" // (ignore case) or the servlet will print out "submit" parameter. // CONSTRUCTOR: no constructor specified (default) // // **************** Methods description ******************************* // void doPost () --> Main method for gathering data and sending back // void doGet () --> Not used. //*********************************************************************** public class FormHandler extends HttpServlet { /** ********************************************************** * doPost() * gathering data and sending back to browser ************************************************************ */ public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // first, set the "content type" header of the response res.setContentType ("text/html"); //Get the response's PrintWriter to return text to the client. PrintWriter toClient = res.getWriter (); String para; Enumeration paraNames = req.getParameterNames (); toClient.println (""); toClient.println (""); toClient.println ("Simple Form Handler"); toClient.println (""); toClient.println (""); toClient.println (""); toClient.println ("

Generic Form Handler

"); toClient.println ("

"); toClient.println ("The following table lists all parameter names and"); toClient.println ("their values that were submitted from your form."); toClient.println ("

"); toClient.println ("

Designed and written by:

"); toClient.println ("

     Jeff Offutt & Quansheng Xiao, Sept 2001

"); toClient.println (""); toClient.println ("

"); toClient.println (""); toClient.println (""); toClient.println (" "); toClient.println (" "); while (paraNames.hasMoreElements ()) { // For each parameter name. para = (String)paraNames.nextElement (); if (!para.equalsIgnoreCase ("submit")) { toClient.println (" "); toClient.println (" "); String[] values = req.getParameterValues (para); if (values != null && !values[0].equals("")) toClient.println (" "); else toClient.println (" "); for (int i = 1; i < values.length; i++) { if (!values[i].equals("")) { toClient.println (" "); toClient.println (" "); toClient.println (" "); } } } } toClient.println ("
Parameter"); toClient.println (" Value"); toClient.println ("
" + para + "" + values[0] + "
 
 " + values[i] + "
"); toClient.println (""); toClient.println (""); toClient.println (""); toClient.println (""); // Close the writer; the response is done. toClient.close(); } //end of doPost() /** ***************************************************** * doGet() * not used ********************************************************* */ public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); //Get the response's PrintWriter to return text to the client. PrintWriter out = res.getWriter(); String title = "This is from FormHandler Servlet"; out.println(""); out.println(""); out.println("" + title + ""); out.println(""); out.println(""); out.println("

" + title + "

"); out.println("

Please run the Servlet from FormHandler.html."); out.println(""); out.println(""); out.close(); } // End doGet() } // End FormHandler class