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 indoGet
anddoPost
methods. - Use
HttpServletRequest.getParameterNames
to get anEnumeration
of parameter names. - Use
HttpServletRequest.getParameterValues(paramName)
to get the parameters values.
Let’s see the simple code snippets that follow:
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
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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:
http://myhost:8080/jcgsnippets/jcgservlet?param1=paramvalue1¶m2=paramvalue2a¶m2=paramvalue2b
Output:
param2
paramvalue2a
paramvalue2b
param1
paramvalue1
This was an example on how to get all Request Parameters in Servlet.
I’am copy and past this code but when insert manual parameters Nothing happen
I’am copy and past this code but when insert manual parameters Nothing happen
I’am copy and past this code but when insert manual parameters Nothing happen