如何使用JSP传递复选框数据?
当需要选择多个选项时,将使用复选框。
以下是带有两个复选框的表单的示例HTML代码CheckBox.htm。
<html>
<body>
<form action = "main.jsp" method = "POST" target = "_blank">
<input type = "checkbox" name = "maths" checked = "checked" /> Maths
<input type = "checkbox" name = "physics" /> Physics
<input type = "checkbox" name = "chemistry" checked = "checked" /> Chemistry
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>上面的代码将产生以下结果-
以下是main.jspJSP程序,用于处理Web浏览器为复选框按钮提供的输入。
<html>
<head>
<title>Reading Checkbox Data</title>
</head>
<body>
<h1>Reading Checkbox Data</h1>
<ul>
<li><p><b>Maths Flag:</b>
<%= request.getParameter("maths")%>
</p></li>
<li><p><b>Physics Flag:</b>
<%= request.getParameter("physics")%>
</p></li>
<li><p><b>Chemistry Flag:</b>
<%= request.getParameter("chemistry")%>
</p></li>
</ul>
</body>
</html>上面的程序将产生以下结果-
读取复选框数据
数学标志::开
物理标志::null
化学标志::开