HTML form processing servlet

We next look at a servlet that processes HTML form data. Exaample 1 and Example 2 show the HTMLFormHandler servlet. Example 1 package its...

We next look at a servlet that processes HTML form data. Exaample 1 and Example 2 show the HTMLFormHandler servlet.

Example 1
package itso.servjsp.servletapi;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HTMLFormHandler extends HttpServlet {
public void init (ServletConfig srvCfg) throws ServletException {
Example 2
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html"); //must be before first ref to PrintWriter
PrintWriter out = res.getWriter();
out.println("<H2>Servlet API Example - HTMLFormHandler</H2><HR>");
//Retrieving the single-value parameters
out.println("Hi <B>" + req.getParameter("firstname") + ",</B><P>");
out.println("I see you are a <B>" + req.getParameter("title") + ",</B><P>");
out.println("And have worked with the following tools: <BR>");
//Retrieving the multi-value parameters
String vals[] = (String []) req.getParameterValues("tools");
if (vals != null) {
for(int i = 0; i<vals.length; i++)
out.println("<B>" + vals[i] + "</B><BR>");
else out.print;n("<B> None </B><BR>");
getReqInfo(req, out); //gets the standard request information
public void getReqInfo(HttpServletRequest req, PrintWriter out)
throws ServletException, IOException {
out.println("<H4><B>Additional Request Information:</B></H4>");
out.println("<B>Request method:</B> " + req.getMethod() + "<BR>");
out.println("<B>Request URI:</B> " + req.getRequestURI() + "<BR>");
out.println("<B>Request protocol:</B> " + req.getProtocol() + "<BR>");
out.println("<B>Request scheme:</B> " + req.getScheme() + "<BR>");
out.println("<B>Servlet path:</B> " + req.getServletPath() + "<BR>");
out.println("<B>Servlet name:</B> " + req.getServerName() + "<BR>");
out.println("<B>Servlet port:</B> " + req.getServerPort() + "<BR>");
out.println("<B>Path info:</B> " + req.getPathInfo() + "<BR>");
out.println("<B>Path translated:</B> " + req.getPathTranslated() + "<BR>");
out.println("<B>Character encoding:</B> "+req.getCharacterEncoding()+ "<BR>");
out.println("<B>Query string:</B> " + req.getQueryString() + "<BR>");
out.println("<B>Content length:</B> " + req.getContentLength() + "<BR>");
out.println("<B>Content type:</B> " + req.getContentType() + "<BR>");
out.println("<B>Remote user:</B> " + req.getRemoteUser() + "<BR>");
out.println("<B>Remote address:</B> " + req.getRemoteAddr() + "<BR>");
out.println("<B>Remote host:</B> " + req.getRemoteHost() + "<BR>");
out.println("<B>Authorization scheme:</B> " + req.getAuthType() + "<BR>");
} //end of class
Request object handling

So far, all of our servlet examples have only used the response object, but not the request object. This example shows how to process the data in the request. We assume that this servlet is always called using a POST request, and have therefore chosen to implement the doPost request handling method.

doPost method

Incidentally, this servlet has been designed to handle the particular type of request from the HTML page that was generated in the previous servlet example. In that HTML page, the user could fill out information in the form and submit it. The action in the HTML form causes the HTMLFormHandler
servlet to be invoked, and the doPost request handler method to be called:
In the doPost method, we handle the HttpServletResponse in the same way as before, except that this time, we are also handling the HttpServletRequest.

Getting form values

We use the getParameter method of the request to extract the values of the request parameters (name/value fields passed in from the HTML page). We extract parameters named firstname and title from the request:
These are two of the input fields that were passed from the HTML form. The getParameter method requires as an argument the name of the parameter that we want to extract (so it must be known), and returns the value of that parameter, or null. To get a list of the all parameter names, we could use the getParameterNames method. This method returns an enumeration of all the parameter names in the request, which we could then iterate through to get the individual parameter values.

To extract the value of the tools parameter, however, we must apply a slightly different technique. The tools’ parameter is a multi-value input field (in this case, a checkbox). Because there could be more than one value to extract, we use the getParameterValues method, which returns an array of values.

Best Online Tutorials | Source codes | Programming Languages: HTML form processing servlet
HTML form processing servlet
Best Online Tutorials | Source codes | Programming Languages
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content