• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Examples » Servlet for handling HTTP GET request Example
Next →
← Prev

Servlet for handling HTTP GET request Example

By Dinesh Thakur

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="https://ecomputernotes.com: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 () MethodNote 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.

You’ll also like:

  1. Servlet for handling HTTP POST request Example
  2. Servlets Client HTTP Request
  3. What happens when our servlet program gets first or Other than First Request from browser window?
  4. Handling Exceptions in Servlet
  5. How Servlet handling multiple post Requests
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Servlet Tutorials

Servlet Tutorials

  • Servlet - Home
  • Servlet - Types
  • Servlet - Advantages
  • Servlet - Container
  • Servlet - API
  • Servlet - Chaining
  • Servlet - Life Cycle
  • Servlet - Developement Way
  • Servlet - Servlet Vs CGI
  • Servlet - Server Side
  • Servlet - HttpServletRequest
  • Servlet - Cookies Advantages
  • Servlet - JDBC Architecture
  • Servlet - HttpServlet
  • Servlet - HttpServletResponse
  • Servlet - Web Technology
  • Servlet - Applet Communication
  • Servlet - Html Communication
  • Servlet - HTTP POST Request
  • Servlet - HTTP GET Request

Other Links

  • Servlet - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW