实例1:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form id="form1" action="reuset1.jsp" method="post" > 用户名:<br/> <input type="text" name="username"> <hr/> 性别:<br/> 男:<input type="radio" name="gender" value="男"> 女:<input type="radio" name="gender" value="女"> <hr/> 喜欢的颜色:<br/> 红色:<input type="checkbox" name="color" value="红色"> 绿色:<input type="checkbox" name="color" value="绿色"> 蓝色:<input type="checkbox" name="color" value="蓝色"> <hr/> 来自的国家:<br/> <select name="country"> <option value="中国"> 中国</option> <option value="美国">美国</option> <option value="俄罗斯">俄罗斯</option> </select> <hr/> <input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html>
显示如下:
处理表单
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("username"); String[] color = request.getParameterValues("color"); %> 姓名:<%=name %> <hr/> 喜欢的颜色:<%for(String c : color) {out.println(c + "");}%> </body> </html>
结果
注意获取get的方式中文参数,比较复杂,需要借助于URLDecoder类进行转码,或者重新编码或解码。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String rawName = request.getParameter("username"); //将字符串使用ISO-8859-1分解成字节数组 byte[] rawBytes = rawName.getBytes("ISO-8859-1"); //将字节数组重新解码成字符串 String username = new String(rawBytes,"UTF-8"); %> 原始查询字符串:<%=rawName %><hr/> 重新编码解码的字符串:<%=username %> </body> </html>
结果:
时间: 2024-10-13 03:13:26