// 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(""); 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("
Parameter"); toClient.println(" | Value"); toClient.println(" |
---|---|
" + para + " | "); String[] values = request.getParameterValues(para); if (values != null && !values[0].equals("")) toClient.println("" + values[0] + " | "); for (int i = 1; i < values.length; i++) { if (!values[i].equals("")) { toClient.println(" |
"); toClient.println(" | " + values[i] + " |
Please run the servlet from formHandler.html."); out.println(""); out.println(""); out.close(); } // End doGet() } // End formHandler class