In the previous post, you are similar to the Servlet Context Parameter Example. This tutorial will show you how to build servlet HTTP request parameters example. The HTTP request parameters are passed along with the request. The HTTP request parameters could be passed by two ways. One way is sent as part of the URL and another way is sent as part of of the body of an HTTP request. This is fifth post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.
Table of contents:
1. Project structure
2. Maven Dependency
3. Servlet HTTP Request Parameters
4. Deploy Servlet HTTP Request Parameters Example
Project structure

Maven Dependency
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> |
Servlet HTTP Request Parameters
The request.getParameter() method is used to get the HTTP request parameters from the request and returns a string or return null if the parameter doesn’t exist. We can also get an array of parameters with request.getParameterValues() which returns an array of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.javabycode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; @WebServlet("/param") public class ServletParamExample extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Get parameters from request String username = req.getParameter("username"); String password = req.getParameter("password"); String[] profileArray = req.getParameterValues("profileArray"); // Print the response on browser resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.write("<html><body>"); out.write("<h2>You just type input:</h2>"); out.write("<p>username: " + username + "</p>"); out.write("<p>password: " + password + "</p>"); out.write("<p>profileArray: " + Arrays.toString(profileArray) + "</p>"); } } |
How to work with array of parameters in Servlet
If you pass the request parameters via URL you will type input on URL such as:
URL: …/servlet-param/param?profileArray=US&profileArray=California
String[] profileArray= req.getParameterValues(“profileArray”);
Deploy Servlet HTTP Request Parameters Example
Here, we deploy file war on application server Tomcat 8 then access the address below
The URL is without parameters: http://localhost:8080/servlet-param/param
We will not receive any Servlet HTTP Request Parameters, here is the result.
The URL is with parameters: http://localhost:8080/servlet-param/param?username=javabycode&password=123&profileArray=US&profileArray=California
We will receive all Servlet HTTP Request Parameters, Here is the result

That’s all. You can also find other servlet example in the series of Java Servlet Example.
Download complete source code, please click link below
Servlet-Parameters-Example.zip (260 downloads)
Happy leanrning!