CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > How To Read Servlet Parameters in Java: A Step-by-Step Guide

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

How To Read Servlet Parameters in Java: A Step-by-Step Guide

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 4 min read · 0 Comments
Share: in X

Reading servlet parameters is a common task when handling HTTP requests. Parameters can be part of the request URL (query parameters) or included in the request body (for POST requests). In this step-by-step guide, we’ll explore how to read servlet parameters using Java servlet, focusing on a practical example that you can execute to understand the process.

Table of Contents

  • Prerequisites
  • Read Servlet Parameters Sent Using GET and POST Method
  • Using GET Request
    • Step 1: Create a Servlet Class
    • Step 2: Deploy to Servlet Container
    • Step 3: Access the Servlet
  • Using POST request
    • Create html form postData.html
    • Create a servlet class with doPost(…) method
    • Open postData.html file
  • Conclusion

Prerequisites

  1. Java Development Kit (JDK): Ensure that you have Java installed on your machine.
  2. Servlet Container: Set up a servlet container such as Apache Tomcat.

Read Servlet Parameters Sent Using GET and POST Method

Using GET Request

Step 1: Create a Servlet Class

Create a new Java class that extends HttpServlet to handle HTTP requests. This example uses the @WebServlet annotation for simplicity.

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/parameter-reader")
public class ParameterReaderServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("text/html");

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

        PrintWriter out = response.getWriter();

        out.println("<html><body>");
        out.println("<h2>Reading Servlet Parameters</h2>");
        out.println("<p>First Name: " + (firstName != null ? firstName : "Not provided") + "</p>");
        out.println("<p>Last Name: " + (lastName != null ? lastName : "Not provided") + "</p>");
        out.println("</body></html>");
    }
}

Step 2: Deploy to Servlet Container

Compile the Java class and deploy it to your servlet container. Ensure the web application context is configured appropriately.

If you are using an Eclipse then just right click on your project and click on Run As -> Run on Server.

Step 3: Access the Servlet

Open your web browser and navigate to the following URL:

http://localhost:8080/ReadServletParameter/parameter-reader

The output would be following:

Run with default output servlet parameter

When we provide the firstName and lastName value in the URL like:

http://localhost:8080/ReadServletParameter/parameter-reader?firstName=Virat&lastName=Kohli

The servlet will read the “firstName” and “lastName” parameters from the URL and display them in the HTML response.

reading servlet parameter

Using POST request

To read data using POST request, first we need to create a HTML form and send data to servlet using POST request then we can read using the same method above.

Create html form postData.html


<html>
<head>
</head>
<body>
	<form action="./parameter-reader-post" method="POST">
		First Name: <input type="text" name="firstName" /> 
		Last Name: <input type="text" name="lastName" /> 
			<input type="submit" value="Submit" />
	</form>
</body>
</html>

Create a servlet class with doPost(…) method

import java.io.IOException;
import java.io.PrintWriter;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/parameter-reader-post")
public class ParameterReaderPostServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("text/html");

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

        PrintWriter out = response.getWriter();

        out.println("<html><body>");
        out.println("<h2>Reading Servlet Parameters</h2>");
        out.println("<p>First Name: " + (firstName != null ? firstName : "Not provided") + "</p>");
        out.println("<p>Last Name: " + (lastName != null ? lastName : "Not provided") + "</p>");
        out.println("</body></html>");
    }
}

Open postData.html file

To open html file, type following url in your browser:

http://localhost:8080/ReadServletParameter/postData.html

It will display a html form. Then type the first name and last name:

sending data to servlet to read parameter

After clicking the Submit button, it will print the output:

read servlet parameter value sent from form

We are able to read the servlet parameter values, using GET and POST methods.

In the get method, the data his displayed in the URL but using the POST method the data is hidden in the URL hence, using POST method is more secure.

Always prefer to use POST method while sending the data from client to server.

You can learn more about HTTP Methods in this article.

Important

While sending data from client to server whether it is using GET or POST method, the parameter name is case sensitive. In our example, firstName in the URL parameter or form name, should be the exact value that we are using in our servlet getParameter(…) method. Otherwise, it will display null value.

Conclusion

In this blog post, we’ve explored how to read servlet parameters using HttpServletRequest, with practical examples demonstrating data transmission through both the GET and POST methods.

Related Posts:

  • Difference Between JDK JRE and JVM
  • Install and Set Up Java Development Environment
  • A Simple Servlet Program in Java
  • How to Pass Data from JSP to Servlet
  • Control Statements in Java
  • Spring Boot Thread Pool Configuration: Optimizing…
Tags:javaserver-side-prograingservlet
Was this article helpful?
← Previous Articlejavax.servlet package
Next Article →Cookie in Servlet

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap