// Example Form Handler Servlet. You may use to help with assignments. // 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 -- Echo 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 "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() * gather data and respond to browser ************************************************************ */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // first, set the "content type" header of the response response.setContentType ("text/html"); //Get the response's PrintWriter to return text to the client. PrintWriter toClient = response.getWriter (); String para; Enumeration paraNames = request.getParameterNames(); toClient.println(""); toClient.println(""); toClient.println(" Generic 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(""); 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 = request.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 request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //Get the response's PrintWriter to return text to the client. PrintWriter out = response.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