Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript Servlet
i want to pass a list from the jsp page to a servlets, i need details.
 
i want to pass the fruits[i] list in my code to servlets.
 
following is my jsp code:
 
====================================
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function checkBoxValidation()
{
for(var i=0; i < document.form1.fruit.length; i++)
{
if(!document.form1.fruit[i].checked)
{
alert("Please Select objects ");
return false;
}
else
{
alert("Click OK to display your selected objects list");
return true;
}
}
}
</script>
<title>JSP Multiple Checkbox</title>
</head>
<body>
<form name="form1" onsubmit="checkBoxValidation()">
<h3>Select Object to Compose Service</h3>
<p><input type="checkbox" name="fruit" value="Temperature"/>temperature</p>
<p><input type="checkbox" name="fruit" value="Humidity"/>humidity</p>
<p><input type="checkbox" name="fruit" value="Smoke"/>Smoke</p>
<p><input type="checkbox" name="fruit" value="floor"/>floor</p>
<p><input type="checkbox" name="fruit" value="date"/>date</p>
<p><input type="checkbox" name="fruit" value="time"/>time</p>
<p><input type="submit" value="submit"/>
 
</form>
<%
String fruits[]= request.getParameterValues("fruit");
request.setAttribute("listName",fruits);
if(fruits != null)
{
%>
<h4> You have selected following Objects </h4>
<ul>
<%
for(int i=0; i<fruits.length; i++)
{
%>
<li><%=fruits[i]%></li>
<%
}
%>
</ul>
<%
}
%>
</body>
</html>
Posted 5-Jun-13 6:26am
Comments
Prasad Khandekar at 5-Jun-13 15:13pm
   
Set the action attribute of your form to a URL mapped to your servlet and also add a method="post" attribute to your form. That way when user is going to click on the submit button the form will get posted to the servlet. In the servlet you can use response.getParameterValues("fruits") to retrieve the values and process the way you want. After processing you may forward to the same JSP or a new JSP.
 
Please also go through this (http://javapapers.com/servlet/what-is-servlet-mapping/) article to know more about servlet mapping.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 5 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100