a.jsp---- <%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" errorPage="" %> <html> <head> <title>JSP练习</title> </head> <body> <form action="b.jsp" method="post" name="frmName"> <table width="400" border="1" align="center" cellspacing="0"> <tr> <td width="80" height="25" scope="col" align="left">姓名</td> <td height="25" scope="col" align="left"><input type="text" id="name" name="name" style="border:#0000FF12px;"></td> </tr> <tr> <td width="80" height="25">性别</td> <td height="25"><input type="radio" name="sex" id="sex" value="男">男 <input type="radio" name="sex" id="sex" value="女">女 </td> </tr> <tr> <td width="80" height="25">喜欢的歌手</td> <td height="25"> <input type="checkbox" name="singer" id="singer" value="张歌手">张歌手 <input type="checkbox" name="singer" id="singer" value="李歌手">李歌手 <input type="checkbox" name="singer" id="singer" value="黄歌手">黄歌手 <input type="checkbox" name="singer" id="singer" value="王歌手">王歌手 </td> </tr> <tr> <td height="25" colspan="2" align="center"> <input type="submit" name="submit" id="submit" value="提交"> <input type="reset" name="reset" id="reset" value="重置"> </td> </tr> </table> </form> </body> </html>
b.jsp <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <!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>Jsp练习</title> </head> <%! public String getGBString(String src){ try{ if (src == null) return null; else{ src = new String(src.getBytes("ISO-8859-1"), "GBK"); return src; } } catch (Exception e){ return null; } } %> <body> <% String name = getGBString(request.getParameter("name")); String sex = getGBString(request.getParameter("sex")); String singers[] = request.getParameterValues("singer"); out.println("姓名:" + name); out.println("性别: " + sex); int length = java.lang.reflect.Array.getLength(singers); for (int i = 0; i < length; i++){ out.println(getGBString(singers[i])); } %> </body> </html>
时间: 2024-08-31 01:06:19