Java/JSP/Form CheckBox
Jsp Form Action: Design Your Sandwich
<HTML>
<HEAD>
<TITLE>Design Your Sandwich!</TITLE>
</HEAD>
<BODY>
<%
String cheese = "", turkey = "", ham = "";
String check1 = "", check2 = "", check3 = "";
if(request.getParameter("radios") != null) {
if(request.getParameter("radios").equals("cheese")){
cheese = "CHECKED";
check2 = "CHECKED";
}
if(request.getParameter("radios").equals("turkey")){
turkey = "CHECKED";
check1 = "CHECKED";
check3 = "CHECKED";
}
if(request.getParameter("radios").equals("ham")){
ham = "CHECKED";
check1 = "CHECKED";
check2 = "CHECKED";
check3 = "CHECKED";
}
}
%>
<H1>Design Your Sandwich!</H1>
<FORM METHOD="POST">
<TABLE BORDER="1" ALIGN="LEFT">
<TR>
<TH>
Sandwich
</TH>
</TR>
<TR>
<TD>
<INPUT TYPE="radio" NAME="radios" VALUE="cheese"
<%= cheese %>
>
Cheese Sandwich
<BR>
<INPUT TYPE="RADIO" NAME="radios" VALUE="turkey"
<%= turkey %>
>
Turkey Sandwich
<BR>
<BR>
<INPUT TYPE="RADIO" NAME="radios" VALUE="ham"
<%= ham %>
>
Ham Sandwich
<BR>
</TD>
</TR>
</TABLE>
<TABLE BORDER="1" ALIGN="LEFT">
<TR>
<TH>
Ingredients
</TH>
</TR>
<TR>
<TD>
<INPUT TYPE="CHECKBOX" NAME="check1" VALUE="check1"
<%= check1 %>
>
Meat
<BR>
<BR>
<INPUT TYPE="CHECKBOX" NAME="check2" VALUE="check2"
<%= check2 %>
>
Cheese
<BR>
<BR>
<INPUT TYPE="CHECKBOX" NAME="check3" VALUE="check3"
<%= check3%>
>
Lettuce
</TD>
</TR>
</TABLE>
<BR CLEAR="ALL">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>
Jsp Form CheckBox Demo
//File: index.html
<HTML>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<BODY bgcolor="white">
<FORM TYPE=POST ACTION=checkresult.jsp>
<BR>
<font size=5 color="red">
Check all Favorite fruits: <br>
<input TYPE=checkbox name=fruit VALUE=apples> Apples <BR>
<input TYPE=checkbox name=fruit VALUE=grapes> Grapes <BR>
<input TYPE=checkbox name=fruit VALUE=oranges> Oranges <BR>
<input TYPE=checkbox name=fruit VALUE=melons> Melons <BR>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</font>
</FORM>
</BODY>
</HTML>
//////////////////////////////////////////////////////////////
//File: checkresult.jsp
<html>
<!--
Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<body bgcolor="white">
<font size=5 color="red">
<%! String[] fruits; %>
<jsp:useBean id="foo" scope="page" class="checkbox.CheckTest" />
<jsp:setProperty name="foo" property="fruit" param="fruit" />
<hr>
The checked fruits (got using request) are: <br>
<%
fruits = request.getParameterValues("fruit");
%>
<ul>
<%
if (fruits != null) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (util.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
<br>
<hr>
The checked fruits (got using beans) are <br>
<%
fruits = foo.getFruit();
%>
<ul>
<%
if (!fruits[0].equals("1")) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (util.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
</font>
</body>
</html>
Submitting Check Boxes
//File: index.html
<HTML>
<HEAD>
<TITLE>Submitting Check Boxes</TITLE>
</HEAD>
<BODY>
<H1>Submitting Check Boxes</H1>
<FORM ACTION="formAction.jsp" METHOD="post">
<INPUT TYPE="CHECKBOX" NAME="check1" VALUE="check1" CHECKED>
Checkbox 1
<BR>
<INPUT TYPE="CHECKBOX" NAME="check2" VALUE="check2">
Checkbox 2
<BR>
<INPUT TYPE="CHECKBOX" NAME="check3" VALUE="check3">
Checkbox 3
<BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>
//File: formAction.jsp
<HTML>
<HEAD>
<TITLE>Reading Checkboxes</TITLE>
</HEAD>
<BODY>
<H1>Reading Checkboxes</H1>
<%
if(request.getParameter("check1") != null) {
out.println("Checkbox 1 was checked.<BR>");
}
else {
out.println("Checkbox 1 was not checked.<BR>");
}
if(request.getParameter("check2") != null) {
out.println("Checkbox 2 was checked.<BR>");
}
else {
out.println("Checkbox 2 was not checked.<BR>");
}
if(request.getParameter("check3") != null) {
out.println("Checkbox 3 was checked.<BR>");
}
else {
out.println("Checkbox 3 was not checked.<BR>");
}
%>
</BODY>
</HTML>