How do I get parameter names from servlet request?

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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

public class ParameterName extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        PrintWriter pw = res.getWriter();

        // Let's obtains parameters name here!
        Enumeration enumeration = req.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String parameterName = (String) enumeration.nextElement();
            pw.println("Parameter = " + parameterName);

When you call the servlet and pass some parameters you get the parameter name echoed on the web browser.


Parameter = price
Parameter = item
Parameter = txid

Leave a Reply

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