JavaWeb学习总结(十五)Jsp中提交的表单的get和post的两种方式
两者的比较:
Get方式:
将请求的参数名和值转换成字符串,并附加在原来的URL之后,不安全
传输的数据量较小,一般不能大于2KB;
post方式:
数量较大;
请求的参数和值放在HTML的请求头中,安全性较高。
实例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <%@ 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> |
显示如下:
处理表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ 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的中文乱码:
注意获取get的方式中文参数,比较复杂,需要借助于URLDecoder类进行转码,或者重新编码或解码。
使用重新编码的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ 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> |
结果:
使用URLDecoder类的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <%@ 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 rawQueryStr = request.getQueryString(); out.println( "原始查询字符串:" + rawQueryStr + "<hr/>" ); //使用URLDecoder解码字符串 String queryStr = java.net.URLDecoder.decode(rawQueryStr, "UTF-8" ); out.println( "解码后的字符串:" + queryStr + "<hr/>" ); //以&分解查询字符串 String[] paramPairs = queryStr.split( "&" ); for (String paramPair : paramPairs){ out.println( "请求参数的键值对为:" + paramPair + "<br/>" ); String[] nameValue = paramPair.split( "=" ); out.println(nameValue[ 0 ] + "参数值是: " + nameValue[ 1 ] + "<hr/>" ); } %> </body> </html> |
结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了