Java – How To Get All Parameters From URL in Servlet

In this post, we show you how to get all parameter values from URL in Java Servlet.

To do this, we can use request.getParameterMap().keySet() to get all parameter names, and then iterate over these parameter names and get its value.

Let’s take a look at the following code to see how it works.

PostServlet.java
package com.bytenota;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "PostServlet", urlPatterns = {"/post"})
public class PostServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        // get all parameter names
        Set<String> paramNames = request.getParameterMap().keySet();
        
        // iterating over parameter names and get its value
        for (String name : paramNames) {
            String value = request.getParameter(name);
            out.write(name + ": " + value);
            out.write("<br>");
        }
    }

}

Result:
Get all parameter names in Java Servlet

guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x