This example will demonstrate you how to work with checkboxes in JSP.
To do check boxes example in JSP we have the following prerequisites:
-
JDK 5.0 or higher is better(In this example, I have compiled code using Java Version (JDK – jdk1.6.0_20 and JRE – jre6)).
-
Apache Tomcat Server 6.X or latest version. (In this example using Tomcat Apache – 6.0.20).
-
Eclipse (You can use any eclipse available or You like).
If all the prerequisites are satisfied. Now follow the following steps to see the Check Boxes Example in JSP.
after installing apache tomcat in eclipse |
Create a Dynamic Web Project named “checkBoxExample” using Eclipse IDE.
If You are desirous to know how to create a dynamic web project using Eclipse IDE and Apache Tomcat Application Server. Please see this tutorial – How to create a dynamic Project Using Eclipse and Tomcat.
eclipse screen once you have created dynamic project. |
Step 3: Creating the three JSP files
In this step we will create the three JSP files.
1. index.jsp – This page is the welcome page where we will describe the user how to proceed for example.
index.jsp – use the below code and save it as index.jsp
<html>
<head>
<title>Check Boxes Example in JSP</title>
</head>
<body>
Welcome to sitenol.com <br/>
Let Us See Check Box Example Here.<br/>
Click on goToExaple link.<br/>
See multiple check boxes on the next page.<br/>
Select Multiple Check Boxes.<br/>
Click On "Show My Hobbies Button"<br/>
Browser will show the selected hobbies.<br/>
<p style="text-align: left: ;">
<a href="checkBoxInTable.jsp">goToExaple</a>
</p>
</body>
</html>
2. checkBoxInTable.jsp – when user will click on “goToExaple” link this page will be show.
checkBoxInTable.jsp – Copy the below code and save it as checkBoxInTable.jsp in eclipse.
<html>
<head>
<title>Check Boxes Example in JSP</title>
</head>
<body>
<form action="displayHobbies.jsp" method="get">
<p align="left" style="font-weight: bolder;">Choose You Hobbies :
</p>
<table border="0">
<tr>
<td><input type="checkbox" name="internetSurf"
value="I am fond of Internet Surfing." /></td>
<td>I am fond of Internet Surfing.</td>
</tr>
<tr>
<td><input type="checkbox" name="readBooks"
value="I am fond of reading books." /></td>
<td>I am fond of reading books.</td>
</tr>
<tr>
<td><input type="checkbox" name="playTennis"
value="I am fond of Plaing Tennis." /></td>
<td>I am fond of Plaing Tennis.</td>
</tr>
<tr>
<td><input type="checkbox" name="driving"
value="I am fond of Driving." /></td>
<td>I am fond of Driving.</td>
</tr>
</table>
<input type="submit" value="Show My Hobbies" />
</form>
</body>
</html>
3. displayHobbies.jsp – This is the page where we can see output.
Copy the code given below, save it as displayHobbies.jsp in your Eclipse IDE.
<html><head>
<title>Check Boxes Example in JSP</title>
</head>
<body>
<p style="text-align: left: ;">
Hello ! Your Selected Hobbies are :
<br><br><br>
<%
java.util.Enumeration paramNames = request.getParameterNames();
if(paramNames != null){
while(paramNames.hasMoreElements()){
out.println(request.getParameter(
(String)paramNames.nextElement()) +"<br>");
}
}
%>
</p>
</body></html>
So you have done with JSP pages. The screen of Eclipse IDE for your ease is given below.
Check Box Example Using Eclipse and Apache Tomcat |
Step 4 : Deploy and Run Our Application
In the last deploy the war file to Apache tomcat Server. Please wait till server gets started then hit
http://localhost:8080/checkBoxExample/index.jsp
you will receive these three screens after navigating properly one by one..
Result of : http://localhost:8080/checkBoxExample/index.jsp
|
select your hobbies in image |
Final Result for Check Boxes selected in the JSP |
I was doing mistake while retrieving form values. You have used Enumeration class to retrieve vales. I have done.