Get all updates of Java4s
Follow Us: Email 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() ].


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
  • web.xml



  • 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)




You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


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.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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