Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets getparametervalues in java

Related Tutorials


 
 

Share on Google+Share on Google+

getparametervalues in java

Advertisement
We will discuss about getparametervalues in java with an example. The request.getParameterValues() method of request object in servlet application. The request.getParameterValues() method gets value from the HTML form fields and return the array of string type value. The request.getParameterValues() method only takes a String type values.

getparametervalues in java

We will discuss about getparametervalues in java with an example. The request.getParameterValues() method of request object in servlet application. The request.getParameterValues() method gets value from the HTML form fields and return the array of string type value. The request.getParameterValues() method only takes a String type values.

This programs will help a programmer to select multiple option from a list and submit their values, which will be displayed on the page.

Program Description:-

In this example we have created multiple checkboxes in HTML form. This form will help user to select multiple Hobbies, which is recalled in servlet/JSP page by using request.getParameterValues()method of the request object. The printWriter is a print representations of objects to a text-output stream.

Syntax of getparametervalue in servlet:-

String[] values=request.getParameterValues("");

Example

index.html

<html>
<head>
<title>Get parameter value</title>
</head>
<body>
<h2 align="center"><font color="green">HOBBIES</font></h2>
<form action="Onget" method="post">
<table align="center">
<tr>
<td><font>Playing Cricket</font></td>
<td><input type="checkbox" name="hobbies" value="Playing Cricket"></td>
</tr>
<tr>
<td><font>Watching Movies</font></td>
<td><input type="checkbox" name="hobbies" value="Watching Movies"></td>
</tr>

<tr>
<td><font>Listening Songs</font></td>
<td><input type="checkbox" name="hobbies" value="Listening Songs"></td>
</tr>

<tr>
<td><font>Playing Basketball</font></td>
<td><input type="checkbox" name="hobbies" value="Playing Basketball"></td>
</tr>

<tr>
<td></td>
<td> <input type="submit" value="Submit"></td>
</tr>

</table>

</form>
</body>
</html>

GetParameterValuesExample.java

package bean;
import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetParameterValuesExample extends HttpServlet 
{
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
PrintWriter printWriter=response.getWriter();
response.setContentType("text/html");

String[] values=request.getParameterValues("hobbies");
printWriter.println("Selected Values:-"); 
for(int i=0; i<values.length; i++)
{
printWriter.println("<li>"+values[i]+"</li>");
}
printWriter.close(); 
}
}

web.xml

<web-app>

<servlet>
<servlet-name>GetParameterValuesExample</servlet-name>
<servlet-class>bean.GetParameterValuesExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>GetParameterValuesExample</servlet-name>
<url-pattern>/Onget</url-pattern>
</servlet-mapping>

<welcome-file-list> 
<welcome-file>index.html</welcome-file> 
</welcome-file-list>

</web-app>

OutPut:-

The output after clicking on submit button:

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 17, 2013

Related Tutorials

Discuss: getparametervalues in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com