Call the Http Servlet Request Method

Source: Internet
Author: User
Tags stock prices

1. Overview of form data

If you have used a Web search engine, or browsed online bookstores, stock prices, and air tickets, you may find some strange URLs, such as "http: // host/path? User = Marty + Hall & origin = bwi & dest = lax ". The part of the URL following the question mark ("user = Marty + Hall & origin = bwi & dest = lax") is the form data, this is the most common method for sending Web page data to server programs. For GET requests, the form data is appended to the question mark of the URL, as shown in the preceding example). For POST requests, form data is sent to the server using a separate row.

Previously, extracting the required form variables from this type of data was one of the most troublesome tasks in CGI programming. First, GET requests and POST requests have different data extraction methods: For GET requests, data is usually extracted using the QUERY_STRING environment variable. For POST requests, data is generally extracted using standard input. Second, the programmer must be responsible for truncating the variable name-variable value pair at the "&" symbol, and then separating the left side of the variable name equal sign and the right side of the variable value equal sign ). Third, URL anti-encoding must be performed on variable values. When sending data, letters and numbers are sent in the original form, but spaces are converted to the plus sign, and other characters are converted to the "% XX" form, XX is the ASCII or ISO Latin-1 encoded value in hexadecimal notation. For example, if the field value "users" in an HTML form is "~", Hall ,~ Gates, and ~ Mcnealy ", the actual data sent to the server is" users = % 7 Ehall % 2C + % 7 Egates % 2C + and + % 7emnnaly ". Finally, the fourth cause of the difficulty in parsing form data is that the variable value may be omitted, for example, "param1 = val1 between m2 = Hangzhou m3 = val3 "), it is also possible that a variable has more than one value, that is, the same variable may appear more than once, for example, "param1 = val1 between m2 = val2 between m1 = val3 ").

One of the advantages of Java Servlet is that all the above parsing operations can be completed automatically. You only need to simply call the getParameter method of the Http Servlet Request, and provide the form variable name in the call parameters in case sensitive), and the GET Request and POST Request are handled in the same way.

The Return Value of the getParameter method is a string. It is the first time that the variable name specified in the parameter appears, the corresponding value is obtained by Reverse encoding ). If the specified form variable exists but there is no value, getParameter returns an empty string. If the specified form variable does not exist, null is returned. If the form variable may correspond to multiple values, you can use getParametervalues to replace getParameter. GetParametervalues can return a string array.

Finally, although in practical applications, Servlet may only use form variables with known names, it is often useful to obtain a complete list of form variable names in the debugging environment, the getParamerterNames method can be used to conveniently achieve this. GetParamerterNames returns an Enumeration, each of which can be converted to a string that calls getParameter in the Http Servlet Request.

2. Example: read three form variables

The following is a simple example. It reads three form variables param1, param2, and param3 and lists their values in the form of an HTML list. Note that although you must specify the response type including content type, status, and other HTTP header information before sending the response content, the Servlet has no requirements on when to read the request content.

In addition, we can easily make the Servlet to process both GET requests and POST requests. This only requires the doGet method to be called in the doPost method, or overwrite the service method to call methods such as doGet, doPost, and doHead ). In actual programming, This is a standard method, because it requires little extra work, but can increase the flexibility of client encoding.

If you are used to reading POST data through standard input using the traditional CGI method, there is also a similar method in the Servlet, that is, calling getReader or getInputStream on the Http Servlet Request, however, this method is too cumbersome for common form variables. However, this method is required if you want to upload files or POST data through a special client program instead of an HTML form.

 
 
  1. Package hall;
  2.  
  3. Import java. io .*;
  4. Import javax. servlet .*;
  5. Import javax. servlet. http .*;
  6. Import java. util .*;
  7.  
  8. Public class ThreeParams extends HttpServlet {
  9. Public void doGet (HttpServletRequest request,
  10. HttpServletResponse response)
  11. Throws ServletException, IOException {
  12. Response. setContentType ("text/html ");
  13. PrintWriterOut=Response. GetWriter ();
  14. StringTitle="Read three Request Parameters";
  15. Out. println (ServletUtilities. headWithTitle (title)
  16. }
  17.  
  18. Public void doPost (HttpServletRequest request,
  19. HttpServletResponse response)
  20. Throws ServletException, IOException {
  21. DoGet (request, response );
  22. }
  23. }
  1. Introduction to functions of Servlet 2.4
  2. Introduction to multiple Servlet Interfaces
  3. Interface introduction-Servlet Context
  4. Servlet Registration Method
  5. Introduction to Servlet containers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.