Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Handling HTTP get Requests Containing Data - 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 get Requests Containing Data - Servlets

When requesting a document or resource from a Web server, it is possible to supply data as part of the request.

Handling HTTP get Requests Containing Data

 

When requesting a document or resource from a Web server, it is possible to supply data as part of the request. The servlet WelcomeServlet2 of Fig. 30.12 responds to an HTTP get request that contains a name supplied by the user. The servlet uses the name as part of the response to the client.

 

    // Fig. 9.12: WelcomeServlet2.java

    // Processing HTTP get requests containing data.

      // process "get" request from client

package com.deitel.advjhtp1.servlets;

   import javax.servlet.*;

    import javax.servlet.http.*;

    import java.io.*;

    public class WelcomeServlet2 extends HttpServlet {

      protected void doGet( HttpServletRequest request,

      HttpServletResponse response )

      throws ServletException, IOException

      {

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

 

      response.setContentType( "text/html" );

      PrintWriter out = response.getWriter();

 

              // send XHTML document 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 get 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.12 WelcomeServlet2 handles a get request containing data

 

Parameters are passed as name/value pairs in a get request. Line 16 demonstrates how to obtain information that was passed to the servlet as part of the client request. The request object’s getParameter method receives the parameter name as an argument and returns the corresponding String value, or null if the parameter is not part of the request. Line 41 uses the result of line 16 as part of the response to the client.

The WelcomeServlet2.html document (Fig. 30.13) provides a form in which the user can input a name in the text input element firstname (line 17) and click the Submit button to invoke WelcomeServlet2. When the user presses the Submit button, the values of the input elements are placed in name/value pairs as part of the request to the server. In the second screen capture of Fig. 30.13, notice that the browser appended

 

?firstname=Paul

 

to the end of the action URL. The ? separates the query string (i.e., the data passed as part of the get request) from the rest of the URL in a get request. The name/value pairs are passed with the name and the value separated by =. If there is more than one name/value pair, each name/value pair is separated by &.

 

 

    <?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.13: WelcomeServlet2.html -->

 

 

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

    <head>

    <title>Processing get requests with data</title>

</head>

 

 

      <body>

      <form action = "/advjhtp1/welcome2" method = "get">

     

      <p><label>

      Type your first name and press the Submit button

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

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

      </p></label>

 

      </form>

      </body>

      </html>

 


Fig. 30.13 HTML document in which the form’s action invokes Welcome-Servlet2 using alias welcome2 specified in web.xml

 

 

Once again, we use our advjhtp1 context root to demonstrate the servlet of Fig. 30.12. Place WelcomeServlet2.html in the servlets directory created in Section 30.3.2. Place WelcomeServlet2.class in the classes subdirectory of WEB-INF in the advjhtp1 context root. Remember that classes in a package must be placed in the appro-priate package directory structure. Then, edit the web.xml deployment descriptor in the WEB-INF directory to include the information specified in Fig. 30.14. This table contains the information for the servlet and servlet-mapping elements that you will add to the web.xml deployment descriptor. You should not type the italic text into the deployment descriptor. Restart Tomcat and type the following URL in your Web browser:

 

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

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

 

Once again, note that the get request could have been typed directly into the browser’s Address or Location field as follows:

 

http://localhost:8080/advjhtp1/welcome2?firstname=Paul

 

Try it with your own name.


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


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