Repository navigation
Expand file tree
/
Copy pathMyRequest.java
More file actions
76 lines (47 loc) · 1.76 KB
/
Copy pathMyRequest.java
File metadata and controls
76 lines (47 loc) · 1.76 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.itheima.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.UnsupportedEncodingException;
import java.util.Map;
public class MyRequest extends HttpServletRequestWrapper {
private boolean encoded = false;
//编码过滤器调用了此方法
public MyRequest(HttpServletRequest request) {
super(request);
}
public String getParameter(String name){
String[] all = getParameterValues(name);
if (all == null){
return null;
}
return all[0];
}
public String [] getParameterValues(String name){
return getParameterMap().get(name);
}
public Map<String,String[]> getParameterMap(){
//使用了装饰者模式
Map<String ,String[]> map = super.getParameterMap();
if (!encoded){
if ("GET".equalsIgnoreCase(super.getMethod())){
for (Map.Entry<String ,String[]> entry : map.entrySet()){
String [] allValue = entry.getValue();
for (int i=0; i< allValue.length;i++){
String encoding = super.getCharacterEncoding();
if (encoding == null){
encoding = "UTF-8";
}
try {
//以字节为单位,进行编码转换
allValue[i] = new String(allValue[i].getBytes("ISO-8859-1"),encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
encoded = true;
}
}
return map;
}
}