Newsletter

Example on getParameterMap() method of Servlet Request Object

Servlets » on Jan 27, 2013 { 2 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

OngetParameterNames.java

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 key-value 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

Output

 

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Full Stack Java/J2EE & UI Web 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

2 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.

Name*
Mail*
Website



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

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