Newsletter

Example on getParameterValues() method of Servlet Request

Servlets » on Jan 28, 2013 { 9 Comments } By Sivateja

The method getParameterValues() will generally came into picture if there is a chance of getting multiple values for any input parameter, this method will retrieve all of it values and store as string array.

Syntax

String[] values = getParameterValues(“Input Parameter”);

Directory Structure

Files Required

  • index.html
  • OngetParameterValues.java
  • web.xml

index.html

<font face="verdana" size="2px">
     <form action="onGPV" method="post">
     Habits :
        <input type="checkbox" name="habits" value="Reading">Reading
        <input type="checkbox" name="habits" value="Movies">Movies
        <input type="checkbox" name="habits" value="Writing">Writing
        <input type="checkbox" name="habits" value="Singing">Singing
        <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 OngetParameterValues extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        PrintWriter pw=res.getWriter();
        res.setContentType("text/html");

        String[] values=req.getParameterValues("habits");
        pw.println("Selected Values...");    
        for(int i=0;i<values.length;i++)
       {
           pw.println("<li>"+values[i]+"</li>");
       }
       pw.close();    
    }
}

web.xml

<web-app>

    <servlet>
        <servlet-name>ongetParameterValues</servlet-name>
        <servlet-class>java4s.OngetParameterValues</servlet-class>
    </servlet>

    <servlet-mapping>
            <servlet-name>ongetParameterValues</servlet-name>
            <url-pattern>/onGPV</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

9 Responses to “Example on getParameterValues() method of Servlet Request”
  1. Nagendra says:

    What is the difference between getParameterMap() and getParameterValues() ?

  2. jaysheel says:

    sir i am doing the same thing but the checkboxes are dynamically generated and want to get the values of several checkboxes to other servlet.
    Please help as soon as possible

  3. siva reddy says:

    How can i store those values in database

  4. Akanksha says:

    Helpful code.I was stuck since long.
    By the snippet
    String[] values=req.getParameterValues(“habits”);

    It solved.

  5. Bansari Wangde says:

    What changes need to be done if I have multiple html files.Suppose I have to jump from one html to another depending on user choice and then call the .java file then the above code doesnot work. So please let me know what changes are to be done.

  6. deepak says:

    Explanation of each and every topic is simply superb sir.
    thank you sir

  7. vinay satam says:

    nice very useful for begin

  8. keshav jha says:

    How can i store those values in database

  9. Debkumar says:

    great very useful for me…..thank you ,..so mach

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.