Newsletter

Example on getParameterMap() method of Servlet Request Object

Servlets » on Jan 27, 2013 { 3 Comments } By Sivateja

Let us see about getParameterMap() method of servlet request object.  This method is little more useful compared to previous methods [ getParameter(), getParameterNames() ].

Syntax

Map m = request.getParameterMap()

  • getParameterMap() method always returns Map object
  • But how we will get input parameter names and their values ? in the form of key and value pairs
  • Previous methods [ getParameter(), getParameterNames() ] will be used if there is a chance of getting single value for particular parameter name, something for example check the output screen, there we can give only one value (Sivateja) in Name field right ?
  • So what if there is a need of selecting more than one value, for example if you would like your customers to select their habits, here they can have more than one habit right ? so we have to provide check boxes [Means multiple values]

That’s why this getParameterMap() came into picture 🙂

Directory Structure

Files Required

  • index.html
  • OngetParameterMap.java
  • web.xml

index.html

<font face="verdana" size="2px">
    <form action="onGPM" method="post">
       Name:<input type="text" name="name">
       Country:<input type="text" name="country"><br>
       Habits : <br>
           <input type="checkbox" name="habits" value="Reading">Reading<br>
           <input type="checkbox" name="habits" value="Movies">Movies<br>
           <input type="checkbox" name="habits" value="Writing">Writing<br>
       <input type="submit" value="Submit">
    </form>
</font>

OngetParameterNames.java

package java4s;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

public class OngetParameterMap extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        PrintWriter pw=res.getWriter();
        res.setContentType("text/html");

        Map m=req.getParameterMap();
        Set s = m.entrySet();
        Iterator it = s.iterator();

            while(it.hasNext()){

                Map.Entry<String,String[]> entry = (Map.Entry<String,String[]>)it.next();

                String key             = entry.getKey();
                String[] value         = entry.getValue();

                pw.println("Key is "+key+"<br>");

                    if(value.length>1){    
                        for (int i = 0; i < value.length; i++) {
                            pw.println("<li>" + value[i].toString() + "</li><br>");
                        }
                    }else
                            pw.println("Value is "+value[0].toString()+"<br>");

                    pw.println("-------------------<br>");
            }

        pw.close();    
    }
}

Explanation

  • Line number 21,  we are getting input parameter values by using getParameterMap() method, which always returns Map object
  • So we have all input parameter names and its values in the form of keyvalue pairs in the Map object, then how to iterate them ?
  • We don’t have iterator() method in the Map interface so how ? In the Map interface we have entrySet() method which always returns Set object, so first get that Set object [ that’s what we did at line number 22 ]
  • Now iterate that Set object [ line number 23 ]
  • line number 27, we are converting the Set generated value into Map.Entry<String,String[]>, means assume one row(Entry) contains key = value ( little imagination will be needed here 😉 )
  • Finally i am printing the values, hope you can understand that logic (check the output screen too)

web.xml

<web-app>

    <servlet>
        <servlet-name>ongetParameterMap</servlet-name>
        <servlet-class>java4s.OngetParameterMap</servlet-class>
    </servlet>

    <servlet-mapping>
            <servlet-name>ongetParameterMap</servlet-name>
            <url-pattern>/onGPM</url-pattern>
    </servlet-mapping>

   <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
   </welcome-file-list> 

</web-app>

Output

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

3 Responses to “Example on getParameterMap() method of Servlet Request Object”
  1. Kunal Krishna says:

    I prefer using
    Map params = request.getParameterMap(); INSTEAD OF
    Map m=req.getParameterMap();

    the former is more readable and readily available to use.

  2. Narasimha Sarma says:

    Your website makes even a layman understand in simple terms. While you have given example of a String, I request you to please give an example of integer as well as Date too.

    eg. for a String, we can understand the syntax as: String emi = request.getParameter(“emi”); how to write the syntax when the variable is a date and input type=”date”. Also what would be the syntax when inserting into mysql through PreparedStatement. Thanks in advance.

  3. wicheth says:

    Excellent

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.