How to Get all Request Parameters in Servlet

 Posted On  | Yashwant Chavan 

In this tutorial, You will learn how to Get all Request Parameters in Servlet. Here we see how to send GET and POST request to servlet and print all the request parameters.

Servlet Jsp - Eclipse Project set up

Get all Request Parameters in Servlet - Eclipse Project Setup

web.xml

Create deployment descriptor (DD) file named as "web.xml" , and put it into "WEB-INF" directory. You can define only one deployment descriptor per web application.

In web.xml file you can declare many servlets. But as of now we have declared only one servlet (HelloController) configuration.

<servlet-name> is mapped <servlet> element to the <servlet-mapping> element.

<servlet-class> is the actual java class e.g.(<servlet-class>com.technicalkeeda.servlet.HelloController</servlet-class>)

<url-pattern> is the request URI mapping (http://localhost:8080/ServletJspExamples/hello)).

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>ServletJspExamples</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>HelloController</servlet-name>
        <servlet-class>com.technicalkeeda.servlet.HelloController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloController</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Standard servlet declarations

package com.technicalkeeda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloController extends HttpServlet {
    private static final long serialVersionUID = 1 L;

    public HelloController() {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.requestParamters(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Calling doPost");
        this.requestParamters(request, response);
    }

    public void requestParamters(HttpServletRequest request, HttpServletResponse response) throws IOException {

        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");

        Enumeration < String > parameterNames = request.getParameterNames();

        while (parameterNames.hasMoreElements()) {

            String paramName = parameterNames.nextElement();

            String[] values = request.getParameterValues(paramName);
            for (String value: values) {
                out.write(paramName + " :- " + value + " ");
            }

        }

        out.close();

    }

}

Deploy and Execute

I am using postman to verify the output.

Send GET request

A GET request appends form data to the end of the URL.

GET request parameters

send POST request

A POST request includes form data in the body of request.

POST request parameters


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0539 seconds.