4.1 Form Data Overview If you have used the web search engine, or browse online bookstores, stock prices, ticket information, may pay attention to some weird URLs, such as "http: // host / path? User = marty Hall & Origin = BWI & DEST = LAX. This URL is located behind the question mark, ie "User = Marty Hall & Origin = BWI & DEST = LAX" is form data, which is the most common method of sending web page data to the server program. For the GET request, the form data is attached to the question mark of the URL (as shown in the above example); for the POST request, form data is sent to the server with a separate row. Previously, from this form of data extraction, the required form variables were one of the troublesome things in CGI programming. First, the data extraction method of the GET request and the POST request is different: for the GET request, it is usually extracted by the Query_String environment variable; for the POST request, the data is generally extracted by standard input. Second, the programmer must be responsible for truncating the variable name-variable pair at the "&" symbol, then separating the variable name (left side) and variable value (equal sign right). Third, the variable value must be performed on the URL anti-encoding operation. Because the letters and numbers are sent in the original form, the spaces are converted into a plus sign, and the other characters are converted into "% xx" form, where XX is the character ASCII represented by hexadecimal (or ISO Latin " 1) Code value. For example, if the domain value named "USERS" in the HTML form is "~ Hall, ~ Gates, and ~ McNEAL", the data sent to the server is "UserS =% 7EHALL% 2C % 7EGATES% 2C AND % 7emcnealy) ". Finally, the fourth causes the fourth result of the analysis of the parsing form data is that the variable value may be omitted (such as "param1 = var_ param2 = & param3 = val3"), and it is possible that a variable has more than one value, that is, the same variable Among the above (such as "param1 = val1 & param2 = VAL2 & param1 = val3). One of the benefits of Java Servlet is that all of the above parsed operations can be done automatically. Just simply call the getParameter method of HTTPSERVLETREQUEST, providing the name of the form variable in the call parameter (case sensitive), and the GET request and the POST request are identical. The return value of the getParameter method is a string, which is the corresponding value specified in the parameter, the corresponding value is obtained by the countercodes (can be used directly). If the specified form variable exists, but no value, getParameter returns an empty string; returns NULL if the specified form variable does not exist. If the form variable may correspond to multiple values, you can use GetParameterValues to replace GetParameter. GetParameterValues can return a string array.
Finally, although the servlet is likely to use the form variables of those known words in practical applications, it is often very useful in debugging environments, which can be easily implemented using the GetParameRnames method. . getParameRNames returns an enumeration, each of which can be converted to a string that calls getParameter. 4.2 Instance: Reading three tables Unit is a simple example, which reads three tables monologous param1, param2, and param3, and lists their values in the form of an HTML list. Note that although the response type (including the content type, status, and other HTTP header information) must be specified before sending the answer content, but the servlet does not require any requirements. In addition, we can easily make the servlet to process the GET request, and can handle the POST request, which only needs to call the Doget method in the dopost method, or override the service method (Service method call doget, dopost, dohead, etc. ). This is a standard method in actual programming because it only needs little additional work, but it can increase the flexibility of client encoding. If you are used to using traditional CGI methods, read POST data through standard input, then there are similar methods in the servlet, namely GetReader or GetInputStream upon httpservletRequest, but this method is too troublesome for ordinary form variables. However, if you want to upload files, or POST data is sent through a dedicated client instead of an HTML form, then this method is used. Note When you read the POST data in a second method, you cannot read these data again with GetParameter.
ThreeParams.java package hall; import java.io *;. Import javax.servlet *;. Import javax.servlet.http *;. Import java.util *;. Public class ThreeParams extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); String title = "request read three parameters"; out.println (ServletUtilities.headWithTitle (title) "
\N" "Next, the program uses a loop to traverse this Enumeration, determine when to end the loop via HasMELEMENTS, use the nextElement to get the enumerations in the enumeration. Since NexTelement returns an Object, the program converts it into a string and uses this string to call GetParameterValues. getParameterValues returns a string array if this array has only one element and is equal to the empty string, indicating that this form variable does not value, and servlet outputs "no value" in a slope body; if the number of elements is greater than 1, how many of this form is more Values, servlets output these values in the form of an HTML list; the servlet puts the variable value into the form in other cases. ShowParameters.java Note that ShowParameters.java uses servletutilities.java described earlier.
package hall; import java.io *;. import javax.servlet *;. import javax.servlet.http *;. import java.util *;. public class ShowParameters extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); String title = "read all request parameters"; out.println (ServletUtilities.headWithTitle (title) "
\N " "Parameter Name | Parameter Value "); Enumeration paramnames = request.getParameterNames (); while (paramnames.hasmorelements ()) {string paramname = (string) Paramnames.nextelement (); out.println (" |
---|---|
" paramname "\N | "); string [] paramvalues = request.getParameterValues (paramname); if (Paramvalues.length == 1) {String paramvalue = paramvalues [0]; if (paramvalue.length () == 0) Out.print (" no value i>); else out.print (paramvalue);} Else {OUT.PRINTLN ("
) {OUT.PRINTLN (" |