该用户从未签到
|
package cn.onecool.filter;5 J- i+ e3 Y9 p0 t
- U7 G4 F$ S/ ^5 R4 w- [0 B
import java.io.IOException;
$ W/ i2 N! N4 {' Vimport java.util.Map;6 M1 Q4 @6 p7 |: i
* ?1 ^5 P( M4 D3 a qimport javax.servlet.Filter;
! J5 A$ M3 _- |2 fimport javax.servlet.FilterChain;- W3 S9 P, Z& j' |0 G- Q
import javax.servlet.FilterConfig;
% z+ g5 j- [5 c: \% uimport javax.servlet.ServletContext;) P8 ]0 a. C/ s8 k& |- t4 Q
import javax.servlet.ServletException;
: J+ y; L7 l8 W" i) @. g6 Qimport javax.servlet.ServletRequest;" _' R' _5 b& t' r9 N. v2 y
import javax.servlet.ServletResponse;% Z5 V8 _2 |$ G [/ h' ^3 X( \ k
import javax.servlet.http.HttpServletRequest;
7 A- o1 q- s$ vimport javax.servlet.http.HttpServletRequestWrapper;
7 h1 q1 m% e$ Y+ \! c- R1 L' d8 b5 a+ b7 q% ?; g
public class EncodingFilte implements Filter {! W2 H* V/ J% m$ _9 A, M4 n
private FilterConfig config = null;7 c& |2 \; {% z" b7 Y
private ServletContext context = null;. x2 E3 f ?0 n8 I( V5 U
private String encode = null;; ^- K: ^3 ]" I1 [( G
& l! \* Y8 a) r public void init(FilterConfig filterConfig) throws ServletException {! ]7 s. T, ]6 q2 Y0 Q' p( a
this.config = filterConfig;* K* v- k5 |& ^" B9 R( K
this.context = filterConfig.getServletContext();
2 c4 ~7 D: U4 _9 i this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");; z- @9 E- {: `& e9 U* k! s
}2 j, Q" l0 d0 \) q
4 [7 s! \( B) D5 E public void doFilter(ServletRequest request, ServletResponse response,
; ?. }1 N+ {" ~: a FilterChain chain) throws IOException, ServletException {+ k% }7 W2 s( c- X! M/ j
/ A8 h% J& V; }/ a8 q // --响应乱码解决. c* I* {" n( a
response.setCharacterEncoding(encode);8 w) k: ?; q1 \. e( |0 H' Q8 f3 w
response.setContentType("text/html;charset=" + encode);
* l, H/ ~% `4 F* _6 r9 R7 c/ a( B( i // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题6 d3 }; F( j" s- r$ L
chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),
5 J& s7 x6 _7 {; C6 z' N" |4 z response);
8 u m" r$ z* h: {) ?! Z }' `" s! Y! ]6 k$ A7 e) v
; I" I& H/ t) v public void destroy() {" [5 @1 u# \- E9 H' s
: _7 g, c1 q7 c, S) W! V+ @ }
" y& v/ v& B# b9 Y6 t! w
4 t4 y# x" h7 w+ } private class MyHttpServletRequest extends HttpServletRequestWrapper {9 s* R+ x" s& Z# i
private HttpServletRequest request = null;3 O( a$ n+ t4 h& n; n4 S
private boolean isNotEncode = true;0 \2 W y. {0 d+ G9 G
- q! Y' L7 S, A% I
public MyHttpServletRequest(HttpServletRequest request) {
C# p) H" V( ?3 X+ T3 ] super(request);
2 S/ N4 L; T9 j* i this.request = request;
) F! P0 i( z2 Q. E/ k$ | }
/ |1 @; y( e' x
6 H6 K! ]! s# t) A" r2 L @Override
: G V6 C" d! d0 n) }7 N public Map<String, String[]> getParameterMap() {
. G) H4 u4 H% {& K' a$ m2 k try {9 ?* D' k g. C7 t+ v
if (request.getMethod().equalsIgnoreCase("POST")) {
" G& g/ E6 z9 h0 y request.setCharacterEncoding(encode);3 C3 J& o2 M; u2 R7 q5 s
return request.getParameterMap();' U$ a" [$ w; j1 m O7 w
} else if (request.getMethod().equalsIgnoreCase("GET")) {
: C6 `8 q' F" t Map<String, String[]> map = request.getParameterMap();5 B. t; p! @* ]* u
if (isNotEncode) {% Y( P6 | Q/ u0 W: O& z1 x) b
for (Map.Entry<String, String[]> entry : map.entrySet()) {
, [: i% q6 ?" A8 b3 i( r6 W4 D String[] vs = entry.getValue();
" a; t/ H/ o+ P) B; v5 @) W0 K) l4 I for (int i = 0; i < vs.length; i++) {0 X; w& }5 Q# A1 D" L1 F
vs[i] = new String(vs[i].getBytes("iso8859-1"),0 A G! i8 ~7 `& G4 X
encode);# V& j: d& @9 q2 h! c8 F
}
8 {! A# ^* x# c }$ k1 m1 Q J. l+ k% {5 q; d% t$ \
isNotEncode = false;. r, J9 f! \% f' K8 w* N
}
9 g- t, j6 r) m+ y/ I( } return map;$ w- C/ c2 |) A: o9 ]
} else {
5 Y. k a8 O! h" \ return request.getParameterMap();
5 _" f/ c5 `# C7 i! c' B: ?% [ }
8 H H2 \+ X0 K4 D+ b3 i4 ?# a } catch (Exception e) {
7 ~+ L# C7 V0 m& @5 ? e.printStackTrace();5 q9 h* g0 C+ n% ?
throw new RuntimeException(e);
' ]9 m) q; ~- x3 X/ i! r$ e% l }6 @1 c9 G! j0 h T; {& [- p
}
3 h6 ~, k1 L7 k, `# K- h' O- u* {! k0 S @3 J+ c) R+ B& ^8 y
@Override
, H% O) |; E- m A public String[] getParameterValues(String name) {$ z. K9 P/ e+ J# s
return getParameterMap().get(name);* M& n3 w, U6 ]' C2 b
}: n, G* N( j7 v' |7 x
& P; l4 }8 w/ H: T5 s* v @Override
- Q* ~, b v3 g. t4 {8 ? public String getParameter(String name) {. E4 E! \+ O& ^
return getParameterValues(name) == null ? null" }2 k+ p" b6 L) }& T
: getParameterValues(name)[0];
1 h: w5 I% H5 G5 r }
; Y6 Z" H- i0 \1 c0 I4 K- `5 @0 |0 B+ s- [2 G
}, b2 p4 q# N! }& T* ]9 J
}4 k* F6 Y; ?% {+ L3 Y* L& r( b- s4 A
! s4 o9 h7 I6 d4 q2 P- n0 }( Z& k& a* V' _% G4 W$ E- R3 J
|
|