Handling HTTP POST Requests
This section develops a servlet that handles an HTIP POST request. The scrvlet is invoked when a form on a Web page is submitted. The example contains two files:

ColorPost.htm defines a Web page and ColorPos Servlet java defines a servlet, The HTML source code for ColorPost.htm is shown in the following listing.,It is identical to ColorGet.htm except that the method parameter for the form tag explicitly specifies that the POST method should be used, and the action parameter for the form tag specifies a different servlet. THe source code (or ColorPostServlet.java is shown in the following listing. The doPost( ) method is overridden to process any HlTP POST requests that are sent to this servlet. It uses the getParameter( ) method of HttpServletRequest to obtain the selection that was made by the user. A response is then formulated Compile the servlet and then test it by performing the same steps described in the previous section

Note that parameters for an HITP POST request are not included as part of the

URL that is sent to the Web server. In this example, the URL sent from the browser to the server is the following:

http; 1/ localhost: uoao I~crvler IColorGetScrvlet

The parameter names And Values arc sent in the body of the HTTP request.

[vfb id=1]

Share This