⼊门:HTML表单与Java后台交互(复选框提交)仅仅给出部分关键代码:html中提交表单用什么属性
HTML form code:
<form action="JavaFormTest" method="post" name="Form">
<p>
清输⼊⽤户名:<input type="text" name="username"/><br />
请输⼊⽤户密码:<input type="password" name="password"/><br />
</p>
请选择你的课程<br />
<p>
<input type="checkbox" name="checkbox" value="⾜球"/>⾜球
<input type="checkbox" name="checkbox" value="篮球"/>篮球
<input type="checkbox" name="checkbox" value="乒乓球"/>乒乓球
<input type="checkbox" name="checkbox" value="排球"/>排球球
</p>
<input type="button" name="but" onclick="submit()" value="提交"/>
</form>
JavaFormTest.java 处理程序doPost 部分:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = Writer();
String username = Parameter("username");
String password = Parameter("password");
String[] list = ParameterValues("checkbox");
if (username.isEmpty())
out.println("name is empty!<br />");
else
out.println("name:" + username + "<br />");
if (password.isEmpty())
out.println("password is empty!<br />");
else
out.println("password:" + password + "<br />");
if (list == null)
out.println("none list");
else {
out.println("list: <br />");
for (int i=0;i<list.length;i++) {
out.println("第 "+(i+1)+" 课程是:"+ list[i]+"<br />");
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。