[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
Request获取请求参数
* Map<String,String[ ]> getParameterMap():获取所有参数Map集合 //遍历 Map<String,
String[]> map = req.getParameterMap(); for (String key : map.keySet()) {
System.out.print(key + ":"); // 获取值 String[] values = map.get(key); for (String
value : values) { System.out.print(value + " "); } System.out.println(); }
* String[ ] getParameterValues(String name)︰根据名称获取参数值(数组)
* String getParameter(String name):根据名称获取参数值(单个值)
乱码处理
post方式
//设置字符输入流的编码 request.setCharacterEncoding("utf-8");
对于get方式,他与post方式获取参数的原理不同,不能通过上述方式处理乱码(据说tomcat8已经解决了这个问题,后续内容不用理会)
先说get方式的乱码原因:对服务器发起请求时,浏览器会把这些汉字进行url编码,tomcat接收到这些字符时,会对这些字符进行解码,默认用的不是utf-8,而是iso-8859-1,而且内部并没有提供修改解码方式的一个接口。
URL编码的步骤
1.将字符串按照编码方式转为二进制
2.每个字节转为2个16进制数并在前边加上%
方案:先对得到的数据以iso-8859-1方式进行编码,得到字节数据,再对字节数组以utf-8解码就得到正确文字。
byte[] bytes = username.getBytes(StandardCharsets.iso-8859-1); username = new
string(bytes,standardCharsets.UTF-8);
请求转发
//请求转发 request.getRequestDispatcher( path: "/req6").forward(request, response);
请求转发资源间共享数据:使用Request对象
* void setAttribute(String name, Object o):存储数据到request域中
* Object getAttribute(String name):根据key,获取值
* void removeAttribute(String name):根据key,删除该键值对
请求转发特点:
* 浏览器地址栏路径不发生变化
* 只能转发到当前服务器的内部资源
* 一次请求,可以在转发的资源间使用request共享数据