Reading multi valued parameters in a servlet

In this post, you will learn:

  • How to read multi valued parameters in a servlet?
  • In which case multi valued parameters are required?

Need of multi valued parameters in a servlet?

Sometimes input forms have check boxes and combo boxes which allow selection of multiple values. The parameters which are used for such input controls have multiple values.

How multi valued parameters can be used in servlet?

ServletRequest interface provides getParameterValues() method for reading all the values of a request parameter. It has following syntax:

public String[] getParameterValues();

Practical example of multi valued parameters:

First, the index.html which have six check boxes all of which can be selected by the client. From this html, a request is sent to the enquiryServlet.

<form method="post" action="enquiryServlet">
Name   <input type="text" name="name"> <br/>
MailId <input type="email" name="mailId"> <br/>
I am interested in the following Course(s): <br/>
           <input type="checkbox" name="course" value="Core Java">Core Java<br>
           <input type="checkbox" name="course" value="Advance Java">Advance Java<br>
           <input type="checkbox" name="course" value="Struts 2">Struts 2<br>
           <input type="checkbox" name="course" value="Spring">Spring<br>
           <input type="checkbox" name="course" value="Hibernate">Hibernate<br>
           <input type="checkbox" name="course" value="Hibernate">Web Service<br>
	
<input type="submit" value="submit">		   
</form>

Second the EnquiryServlet, It read the value of single valued as well as multi valued parameters and send them as response to the client.

package com.techmentro.learningpad;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class EnquiryServlet extends HttpServlet
{
	
	public void doPost(
			HttpServletRequest request, 
			HttpServletResponse response)
			throws ServletException, IOException {
		
		//Values of Single valued parameters are  read
		String name=request.getParameter("name");
		String mailId=request.getParameter("mailId");
		//Values of the Multi-valued parameter are read
		String courses[]=request.getParameterValues("course");
		
		//In real application these values will be saved in the database.
		//To confine this example only to the reading of multi-valued parameters, parameters values are simply sent back as response.
		PrintWriter out=response.getWriter();
		response.setContentType("text/html");
		out.println("Hello, <b>"+name+"</b><br/>");
		out.println("You have shown interest in the following course(s):<br/>");
		for(String course:courses)
			out.println("<b>"+course+"</b><br/>");
		
		out.println("Very soon, you will receive the course detial mail from us on the given mailId: <b>"+mailId+"</b>");
		out.close();
		
		
	}
	
	

}

Third, the web.xml. It maps the enquiryServlet URI to the EnquiryServlet class.

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  
 
  <servlet>
    <servlet-name>s1</servlet-name>
    <servlet-class>com.techmentro.learningpad.EnquiryServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>s1</servlet-name>
   <url-pattern>/enquiryServlet</url-pattern>
  </servlet-mapping> 
  
</web-app>

Output screen images of the practical example:

This screen displays the input form generated by index.html in which values are filled by the client. Note that more than one check boxes are selected.

mulitvalued_parameters_screen1

The second screen represents the response which contains all the values submitted by the client.

mulitvalued_parameters_screen2

Its time to recap the main concepts:

  1. In an input form multiple check boxes, and multiple options of a combo box can be selected by the user.
  2. The parameters which are used to provide the value of check boxes and combo boxes may have multiple values.
  3. getParameterValues() method of ServletRequest interface is used to read all the values of a parameter.

Example Source Code

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *