Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Handling HTTP post Requests - Servlets

Chapter: Internet & World Wide Web HOW TO PROGRAM - Servlets: Bonus for Java™ Developers

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Handling HTTP post Requests - Servlets

An HTTP post request is often used to post data from an HTML form to a server-side form handler that processes the data. For example, when you respond to a Web-based survey, a post request normally supplies the information you specify in the HTML form to the Web server.

Handling HTTP post Requests

 

An HTTP post request is often used to post data from an HTML form to a server-side form handler that processes the data. For example, when you respond to a Web-based survey, a post request normally supplies the information you specify in the HTML form to the Web server.

Browsers often cache (save on disk) Web pages so they can quickly reload the pages. If there are no changes between the last version stored in the cache and the current version on the Web, this helps speed up your browsing experience. The browser first asks the server if the document has changed or expired since the date the file was cached. If not, the browser loads the document from the cache. Thus, the browser minimizes the amount of data that must be downloaded for you to view a Web page. Browsers typically do not cache the server’s response to a post request, because the next post might not return the same result. For example, in a survey, many users could visit the same Web page and respond to a question. The survey results could then be displayed for the user. Each new response changes the overall results of the survey.

 

When you use a Web-based search engine, the browser normally supplies the informa-tion you specify in an HTML form to the search engine with a get request. The search engine performs the search, then returns the results to you as a Web page. Such pages are often cached by the browser in case you perform the same search again. As with post requests, get requests can supply parameters as part of the request to the Web server.

 

The WelcomeServlet3 servlet of Fig. 30.15 is identical to the servlet of Fig. 30.12, except that it defines a doPost method (line 12) to respond to post requests rather than a doGet method. The default functionality of doPost is to indicate a “Method not allowed” error. We override this method to provide custom post request processing. Method doPost receives the same two arguments as doGet—an object that implements interface HttpServletRequest to represent the client’s request and an object that implements interface HttpServletResponse to represent the servlet’s response. As with doGet, method doPost throws a ServletException if it is unable to handle a client’s request and throws an IOException if a problem occurs during stream processing.

 

 

      // Fig. 9.15: WelcomeServlet3.java

      // Processing post requests containing data.

      package com.deitel.advjhtp1.servlets;

4

      import javax.servlet.*;

      import javax.servlet.http.*;

      import java.io.*;

    public class WelcomeServlet3 extends HttpServlet {

 

        // process "post" request from client

        protected void doPost( HttpServletRequest request,

        HttpServletResponse response )

        throws ServletException, IOException

        {

        String firstName = request.getParameter( "firstname" );

 

        response.setContentType( "text/html" );

        PrintWriter out = response.getWriter();

                    // send XHTML page to client

         

                    // start XHTML document

                    out.println( "<?xml version = \"1.0\"?>" );

                    out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD " +

                    "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +

                    "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );

                   

                    out.println(

                    "<html xmlns = \"http://www.w3.org/1999/xhtml\">" );

                    // head section of document

        out.println( "<head>" );

        out.println(

        "<title>Processing post requests with data</title>" );

        out.println( "</head>" );

        // body section of document

        out.println( "<body>" );

        out.println( "<h1>Hello " + firstName + ",<br />" );

        out.println( "Welcome to Servlets!</h1>" );

        out.println( "</body>" );

        // end XHTML document

        out.println( "</html>" );

        out.close();  // close stream to complete the page

        }

}

Fig. 30.15  WelcomeServlet3 responds to a post request that contains data.

 

The WelcomeServlet3.html document (Fig. 30.16) provides a form (lines 13– 21) in which the user can input a name in the text input element firstname (line 17), then click the Submit button to invoke WelcomeServlet3. When the user presses the Submit button, the values of the input elements are sent to the server as part of the request. However, note that the values are not appended to the request URL. Note that the form’s method in this example is post. Also, note that a post request cannot be typed into the browser’s Address or Location field and users cannot bookmark post requests in their browsers.

 

            <?xml version = "1.0"?>

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

           

            <!-- Fig. 9.16: WelcomeServlet3.html -->

 

 

            <html xmlns = "http://www.w3.org/1999/xhtml">

            <head>

            <title>Handling an HTTP Post Request with Data</title>

            </head>

            <body>

            <form action = "/advjhtp1/welcome3" method = "post">

           

<p><label>

            Type your first name and press the Submit button

            <br /><input type = "text" name = "firstname" />

            <input type = "submit" value = "Submit" />

            </label></p>

 

            </form>

            </body>

            </html>


Fig. 30.16     HTML document in which the form’s action invokes Welcome-Servlet3 through the alias welcome3 specified in web.xml

We use our advjhtp1 context root to demonstrate the servlet of Fig. 30.15. Place WelcomeServlet3.html in the servlets directory created in Section 30.3.2. Place

 

WelcomeServlet3.class in the classes subdirectory of WEB-INF in the advjhtp1 context root. Then, edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 30.17. Restart Tomcat and type the following URL in your Web browser:

 

http://localhost:8080/advjhtp1/servlets/WelcomeServlet3.html

 

Type your name in the text field of the Web page, then click Submit to invoke the servlet.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.