HttpServlet doGet() Method Example

In this post, we will demonstrate the usage of the HttpServlet class provided doGet() method with an example. the doGet() method is called by the server (via the service method) to allow a servlet to handle a GET request. Generally, we use the doGet() method for getting the information from the server.

Add servlet dependency to pom.xml or classpath

Let's add servlet 4.0.1 dependency to pom.xml:
        <!-- -->
If your project is not a maven project then you can download the jar file and keep in the classpath.

HttpServlet doGet() Method Example

Here we will develop a servlet that handles an HTTP GET request. We will create a Student registration form and the servlet is invoked when a form on a web page is submitted. The example contains a Student registration JSP form defined in a student.jsp and a servlet is defined in
Let's create a student.jsp file which contains a Student registration HTML form code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta charset="ISO-8859-1">
<title>JSP Actions Example</title>

<h1> Student Registration Page</h1>
 <form action="<%= request.getContextPath() %>/StudentServlet" method="get">
  First Name: <input type="text" name="firstName">
  <br> <br> 
  Last Name: <input type="text" name="lastName">
  <br> <br> 
  Email ID: <input type="email" name="emailId">
  <br> <br> 
  Password: <input type="password" name="password"><br>
  <input type="submit" value="register">
Notice that the action parameter of the form tag specifies a URL. The URL identifies a servlet to process the HTTP GET request.
Let's run this example in tomcat server and hit http://localhost:8080/java-servlet-tutorial/student.jsp link into a browser will result below web page:
Let's create file and enter the following code into it. The doGet( ) method is overridden to process any HTTP GET 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.
package net.javaguides.servlet.tutorial.examples;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentServlet extends HttpServlet {

    private static final long serialVersionUID = 1 L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        String emailId = req.getParameter("emailId");
        String password = req.getParameter("password");

        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<h1>Student Resistration Form Data</h1>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");
        printWriter.print("<p> lastName :: " + firstName + "</p>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");
        printWriter.print("<p> firstName :: " + firstName + "</p>");

        System.out.println("firstName :: " + firstName);
        System.out.println("lastName :: " + lastName);
        System.out.println("emailId :: " + emailId);
        System.out.println("password :: " + password);
Let's fill above Student registration form and hit submit button will result below page: