jsp获取多选框组件的值
jsp获取多选框组件的值
1.首先写一个带有多选框的前台页
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="request2.jsp" method="post"> 11 喜欢的颜色:<br/> 12 红:<input type="checkbox" name="color" value="红"/> 13 绿:<input type="checkbox" name="color" value="绿"/> 14 蓝:<input type="checkbox" name="color" value="蓝"/> 15 <hr/> 16 <input type="submit" /> 17 </form> 18 </body> 19 </html>
2.写一个处理页获取多选框组件的值
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 request.setCharacterEncoding("utf-8"); 12 String[] color = request.getParameterValues("color"); 13 %> 14 您喜欢的颜色 15 <% 16 for(String c : color){ 17 out.println(c + " "); 18 } 19 %> 20 </body> 21 </html>