by Dinesh Thakur Category: Basic Servlet Examples

The doGet () method is invoked by server through service () method to handle a HTTP GET request. This method also handles HTTP HEAD request automatically as HEAD request is nothing but a GET request having no body in the code for response and only includes request header fields. To understand the working of doGet () method, let us consider a sample program to define a servlet for handling the HTTP GET request.

A program to define a servlet for handling HTTP GET request

import java.io.*;

import java.util.*;

import javax.servlet.*;

public class ServletGetExample extends HttpServlet

{

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

   {

      PrintWriter out = res.getWriter();

      String login= req.getParameter("loginid");

      String password= req.getParameter("password");

      out.println("Your login ID is: ");

      out.println(login);

      out.println("Your password is: ");

      out.println(password);

      out.close();

   }

}

In this example, the doGet () method of HttpServlet class is overridden to handle the HTTP GET request. The two parameters passed to the doGet () method are req and res, the Objects of HttpServletRequest and HttpServletResponse interface respectively. The req object allows to read data provided in the client request and the res object is used to develop Response for the client request.

The corresponding HTML code for this servlet is as follows

<HTML>

          <BODY>

                  <CENTER>

                         <FORM NAME="Form1" METHOD="post" ACTION="http://localhost:8080/ServletGetExample">

                                 <B>Login ID</B> <INPUT TYPE="text" NAME="loginid" SIZE="30">

                                 <P>

                                 <B>Password</B> <INPUT TYPE="password" NAME="password" SIZE="30">

                                 </P>

                                 <P>

                                 <INPUT TYPE=submit VALUE="Submit".>

                                 </P>

           </BODY>

</HTML>

This HTML code create as web page containing a form

             Web Page Displaying the Form

Enter the required data and press the submit button on the web page. The browser will display the response generated dynamically by the corresponding servlet.

             Output Generated by Servlet using doGet () Method

Note that, the getParameter () method of HttpServletRequest interface is used to retrieve data attached to the URL sent to the server. For example, consider the URL in the address bar of Figure. The string appearing to the right of the question mark known as the query string, contains the parameters for the HTTP GET request.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.