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
- Java Development Kit (JDK): Ensure that you have Java installed on your machine.
- 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:
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.
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:
After clicking the Submit button, it will print the output:
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.