Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Java Servlet :Manipulating multiple value parameters
Previous Home Next

The following example illustrates the use of the getParameterValues method to get all favourite music selected by the user.The code for this servlet is as follows:

index.html

<html>
 <head>
  <title>Check box Demo</title>
 </head>
 <body>
  <form method = "post" action = "music">
  <p>Which type of music you like it ? </p>
  <input type = "checkbox" name ="favouritemusic" 
value = "Rock">
   Rock<br>
  <input type = "checkbox" name ="favouritemusic" 
value = "Jazz">Jazz<br>
  <input type = "checkbox" name ="favouritemusic" 
value = "hiphop">hiphop.<br>
  <input type = "checkbox" name ="favouritemusic" 
value = "pop">pop<br>
  <input type = "checkbox" name ="favouritemusic" 
value = "heavy rock">heavy rock<br>
  <input type ="submit" name= "submit">
  </form>
 </body>
</html>

web.xml

<web-app>
 <servlet>
  <servlet-name>H1</servlet-name>
  <servlet-class>GetParamValues</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>H1</servlet-name>
  <url-pattern>music</url-pattern>
 </servlet-mapping>
</web-app>

GetParamValues.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetParamValues extends HttpServlet{
  protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)throws ServletException,IOException 
{
  String[] values=request.getParameterValues("favouritemusic");
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  if(values!=null)
  {
   int length=values.length;
   for(int i=0;i<length;i++)
   pw.println("<br>Favourite Music : " + values[i]);
  }
 }
 }
Previous Home Next