How do I get parameter names from servlet request?
Category: javax.servlet, viewed: 7703 time(s).
This example show you how to obtain parameter name from servlet request. By calling request.getParameterNames() you will get an Enumeration object by iterating this object you can get the parameter names.
package org.kodejava.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ParameterName extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
public ParameterName() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter pw = response.getWriter();
//
// Let's obtains parameters name here!
//
Enumeration enumeration = request.getParameterNames();
while (enumeration.hasMoreElements()) {
String parameterName = (String) enumeration.nextElement();
pw.println("Parameter = " + parameterName);
}
pw.close();
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}
When you call the servlet and pass some parameters you get the parameter name echoed on the web browser.
http://localhost:8080/java-web-examples/ParameterName?txid=001&item=10&price=1000
Parameter = price
Parameter = item
Parameter = txid
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!