servlet

Get all Request Parameters in Servlet

With this tutorial we shall show you how to get all requests parameters in a Java Servlet. This the most basic step you have to consider when developing a Servelt application because HTTP is based mostly on parameters exchange.

Basically in order to get all Request Parameters in Servlet, one should take the following steps:

  • Create a handleRequest method so you can use it both in doGet and doPost methods.
  • Use HttpServletRequest.getParameterNames to get an Enumeration of parameter names.
  • Use HttpServletRequest.getParameterValues(paramName) to get the parameters values.

 
Let’s see the simple code snippets that follow:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.javacodegeeks.snippets.enterprise;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class GetAllRequestParametersInServlet extends HttpServlet {
 
    private static final long serialVersionUID = -2128122335811219481L;
 
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
        handleRequest(req, res);
    }
 
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        handleRequest(req, res);
    }
 
    public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws IOException {
 
        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");
 
        Enumeration<String> parameterNames = req.getParameterNames();
 
        while (parameterNames.hasMoreElements()) {
 
            String paramName = parameterNames.nextElement();
            out.write(paramName);
            out.write("n");
 
            String[] paramValues = req.getParameterValues(paramName);
            for (int i = 0; i < paramValues.length; i++) {
                String paramValue = paramValues[i];
                out.write("t" + paramValue);
                out.write("n");
            }
 
        }
 
        out.close();
 
    }
 
}

web.xml

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
  version="2.5">
 
    <display-name>JCG Snippets Web Project</display-name>
 
    <servlet>
        <servlet-name>JCG Snippets Application</servlet-name>
        <servlet-class>com.javacodegeeks.snippets.enterprise.GetAllRequestParametersInServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>JCG Snippets Application</servlet-name>
        <url-pattern>/jcgservlet</url-pattern>
    </servlet-mapping>
 
</web-app>

URL:

Want to be a Servlets Master ?
Subscribe to our newsletter and download the Java Servlet Ultimate Guide right now!
In order to help you master programming with Java Servlets, we have compiled a kick-ass guide with all the major servlet API uses and showcases! Besides studying them online you may download the eBook in PDF format!

http://myhost:8080/jcgsnippets/jcgservlet?param1=paramvalue1&param2=paramvalue2a&param2=paramvalue2b

Output:

param2
	paramvalue2a
	paramvalue2b
param1
	paramvalue1

 
This was an example on how to get all Request Parameters in Servlet.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
alnebras
alnebras
7 years ago

I’am copy and past this code but when insert manual parameters Nothing happen

Vivek Bhadiyadra
Vivek Bhadiyadra
3 years ago

I’am copy and past this code but when insert manual parameters Nothing happen

Joe Dunn
Joe Dunn
3 years ago

I’am copy and past this code but when insert manual parameters Nothing happen

Back to top button