从JSP页面获取复选框的values值,通过定义数组,将其值存入数组之中,并显示出来
最简单的就是
代码如下 | 复制代码 |
String strLove = ""; String[] strLoves = (String[])request.getParameterValues("love"); //通过循环读取每个选中项 strLove = strLove.substring(0,strLove.length()-1); |
例
html页面
代码如下 | 复制代码 |
<form action="02.jsp" method="post"> 姓名:<input type="text" name="uname" /> <p> 擅长技术: <input type="checkbox" name="tech" value="J2EE" />J2EE <input type="checkbox" name="tech" value=".NET" />.NET <input type="checkbox" name="tech" value="ASP" />ASP <input type="checkbox" name="tech" value="PHP" />PHP <p> <input type="submit" value="提交" /> </form> |
jsp处理页面
代码如下 | 复制代码 |
<%@page contentType="text/html;charset=GB2312" %> <html> <head> <title>02.jsp</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("GB2312"); String Name=request.getParameter("uname"); //获得参数数组 String Tech[]=request.getParameterValues("tech"); %> <h1>姓名:<%=Name %></h1> <h1>擅长技术: <% //输出数组 int i; for(i=0;i<Tech.length;i++) { %> <%=Tech[i] %> <% } %> </h1> </body> </html> |
上面的代码不分中英文的,如果选项的值中含有中文,在JSP中的处理代码如下:
代码如下 | 复制代码 |
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%! <% //通过循环读取每个选中项 strLove = strLove.substring(0,strLove.length()-1); |
jsp 获得checkbox值并写入数据库,可通过以下方法:
代码如下 | 复制代码 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>test</title> </head> <body> |
保存页面jsp程序
代码如下 | 复制代码 |
<%@ page contentType="text/html; charset=utf-8" language="java" import = "java.util.*, java.text.*,java.sql.*"%> <% String tmp=""; tmp ="insert into Corporation (CorpMode) values('+SCorpMode+'); if(dbc.executeUpdate(tmp)>=0) |