從JSP頁面擷取複選框的values值,通過定義數組,將其值存入數組之中,並顯示出來
最簡單的就是
代碼如下 |
複製代碼 |
String strLove = ""; String[] strLoves = (String[])request.getParameterValues("love");
//通過迴圈讀取每個選中項 for (String love : strLoves) { strLove = strLove + 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="" %> <%! public String chinese(String sText) { try { return new String(sText.getBytes("iso-8859-1"),"gbk"); } catch(Exception e) { return sText; } } %> <% String strLove = ""; String[] strLoves = (String[])request.getParameterValues("love"); //通過迴圈讀取每個選中項 for (String love : strLoves) { strLove = strLove + chinese(love) + ","; } 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> <form name="myform" id="myform" method="post" action="CorAdd.jsp"> <label> <input type="checkbox" name="CorpMode" value="1" />物品 </label> <label> <input type="checkbox" name="CorpMode2" value="2" />人頭 </label> <label> <input type="checkbox" name="CorpMode2" value="3" />其他 </label> <input name="" type="button"onclick="document.myform.action='Submit.jsp?menu=add'" /> </form> </body> </html> |
儲存頁面jsp程式
代碼如下 |
複製代碼 |
<%@ page contentType="text/html; charset=utf-8" language="java" import = "java.util.*, java.text.*,java.sql.*"%> <% if(menu.equals("add")) { String tmp=""; String SCorpMode=""; String[] CorpMode = request.getParameterValues("CorpMode"); if(CorpMode.length>0) { for(int i=0;i<CorpMode.length;i++) { SCorpMode=SCorpMode+CorpMode+","; } SCorpMode=SCorpMode.substring(0,SCorpMode.length()-1);//去掉SID中的最後一個逗號 } tmp ="insert into Corporation (CorpMode) values('+SCorpMode+'); if(dbc.executeUpdate(tmp)>=0) out.println("<script>alert('資訊添加成功!');location.href='CorpAdd.jsp'</script>"); else //out.println("<script>alert('添加失敗!');location.href='CorpAdd.jsp'</script>"); out.print(tmp); } } %> |