HomeJSP

Checkboxes example in jsp

Like Tweet Pin it Share Share Email

This example will demonstrate you how to work with checkboxes in JSP.

To do check boxes example in JSP we have the following prerequisites:

  1. 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)).
  2. Apache Tomcat Server 6.X or latest version. (In this example using Tomcat Apache – 6.0.20).
  3. 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.
Step 1 : Installing/Configuring Apache Tomcat Server In Your Eclipse IDE
Please install Apache Tomcat Server in your eclipse IDE. So that you can test your application without bothering of compiling code and war deploying processes. By configuring Tomcat Apache Server in Your Eclipse IDE,  You can deploy the war file directly to Tomcat Server rather than manually compiling code and deploying the war.
Eclipse has provided inbuilt function to create a war and deploy it to Apache Tomcat or any other Application Server. About this we will discuss in the last when we will go to deploy war of ourcheck – boxes example in JSP.
If you have not installed the Tomcat Server in your eclipse, Please install it to make your task easy. See how to install Tomcat Apache Server in your eclipse IDE.
After successful installation of  Apache Tomcat Application Server you will have image in your eclipse IDE will be as given below.
JSP - Check Boxes Example
after installing apache tomcat in eclipse
You can start and stop the Apache Tomcat Server in Eclipse IDE just by a right mouse click.
Step 2 : Create a Dynamic web project named “checkBoxExample

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.

Once you have created the Dynamic Web Project named“checkBoxExample” after that you need to create the required jsp files to execute our Check Boxes Example in JSP.
Below is the image After creating the Dynamic Web Project “checkBoxExample” in Eclipse.
Check boxes example in jsp
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.

JSP - Examples
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..

JSP - Examples
Result of : http://localhost:8080/checkBoxExample/index.jsp

 

JSP - Examples
select your hobbies in image

 

JSP - Examples
Final Result for Check Boxes selected in the JSP

 

Thats All folks ! We have done with Check Boxes in JSP. There are other ways also to display the selected values in the check boxes.

Comments (1)

Leave a Reply

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