该用户从未签到
|
package cn.onecool.filter;/ W9 ~5 R/ }1 V6 |0 x
- P. h; [& s3 j; D- mimport java.io.IOException;5 B! c' l1 V7 L/ O7 U( k/ @; X
import java.util.Map;
9 ^) W' l2 N9 Z% b, h. Q3 |4 K3 {1 h% C4 q2 v
import javax.servlet.Filter;5 R) p& o; y. b. o' q) X% {8 k/ g
import javax.servlet.FilterChain;: e+ i: q! `7 ^. x
import javax.servlet.FilterConfig;# X3 U' N R1 ~0 @6 W
import javax.servlet.ServletContext;
) D& d1 l$ h- E9 Q# \% `6 h6 g; mimport javax.servlet.ServletException;" o0 z: t8 Y" z0 j0 u
import javax.servlet.ServletRequest;, H0 o* x2 p# k0 C) e
import javax.servlet.ServletResponse;
3 Z$ T6 V* l4 Z8 {3 X7 Dimport javax.servlet.http.HttpServletRequest;
# l! ^- B5 U. O1 L, [0 q1 |5 _8 fimport javax.servlet.http.HttpServletRequestWrapper;3 e; v" c# @$ D+ N. s
; T4 \9 z. I5 l6 k" Q
public class EncodingFilte implements Filter {3 G6 C( h0 L8 P. i2 Y4 \/ a
private FilterConfig config = null;0 H1 a! w$ z0 [- b- z' y: S
private ServletContext context = null;
3 K2 \( Q0 D# J. M1 I private String encode = null;
3 q" |' C2 ~4 n3 K
?; K* d" ^% V: W/ @0 Y public void init(FilterConfig filterConfig) throws ServletException {- ^+ _- g5 X2 C% \9 b; k; e
this.config = filterConfig;
0 B' h& k- |/ g( r5 z" G) ? this.context = filterConfig.getServletContext();
9 d% S0 ]1 I$ M5 Z this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");$ G- S" D, D( {
}
\1 L8 C# G3 J4 Y" }* Z9 V" T% ]0 i$ S* ^$ ]; }
public void doFilter(ServletRequest request, ServletResponse response,
: C4 g( k( p9 ]5 k FilterChain chain) throws IOException, ServletException {0 }9 J! |* C# Z
7 }/ Z3 c0 M5 m# ?6 N, o: u
// --响应乱码解决 J- L# ?, u" A$ _$ l
response.setCharacterEncoding(encode);
/ V& x5 \; [$ ~; R6 Q response.setContentType("text/html;charset=" + encode);
: ^" r2 E: h/ C( c1 p // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
3 r. e8 ?5 T: E7 Q" n! G+ Q1 s chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),
/ r1 ?) k' K( v! u% l8 t response);
( t ~; W8 f6 `, t% ]9 V5 _ }
# Y# l$ I: Q7 l) H( |2 ^" D& d$ m, {( f4 \4 F+ t" k; _
public void destroy() {( B5 z2 l- Z' K, c
) d0 q/ v% ?1 g# E5 S }: q0 S: r+ g: e* c8 x$ l
/ {( D' K0 i8 g# t
private class MyHttpServletRequest extends HttpServletRequestWrapper {* K8 t+ {& D; D* N
private HttpServletRequest request = null;% h" b5 T6 m4 S! [0 k0 g) u6 {5 y$ d
private boolean isNotEncode = true;: k3 ~6 Q& ?" L: u* m7 Z
, g0 c/ v0 q% _$ m" Y. | public MyHttpServletRequest(HttpServletRequest request) {
7 B) g( c2 o0 C7 j3 P2 ?: [ super(request);
' i" M5 B) C. [, _" _# j/ t this.request = request;
8 j @# Z# z/ X }4 Y: g/ V" Z3 \
+ G6 @9 j' X. ]% t* ]6 ^8 c5 q A
@Override
" r7 B) B9 ~4 m. l# t+ b! T1 q public Map<String, String[]> getParameterMap() {
, u6 h2 W. R( n2 M, ]' u/ U try {; ~' s2 X) Q# O8 d: y' F1 b; E/ Q- z$ V
if (request.getMethod().equalsIgnoreCase("POST")) {
% y) ?0 c4 q9 C0 N) @/ I5 q8 z request.setCharacterEncoding(encode);
, R: @+ ]7 c2 R! B3 T# |8 E return request.getParameterMap();7 j* R5 l! r- B$ u# t
} else if (request.getMethod().equalsIgnoreCase("GET")) {$ }$ n+ D. s0 B" D0 R1 V; I7 z
Map<String, String[]> map = request.getParameterMap();+ S& ^' @" }; [+ ?: j8 T' y
if (isNotEncode) {
4 [0 {$ M9 ]" K% K# ] for (Map.Entry<String, String[]> entry : map.entrySet()) {
7 t, W. ?" z3 K) C; O1 u ^ String[] vs = entry.getValue();9 P( }2 j5 M! x5 y+ U$ I) K
for (int i = 0; i < vs.length; i++) {3 Y: ^) B4 R, q7 H& G
vs[i] = new String(vs[i].getBytes("iso8859-1"),
2 V1 o) l* A+ ]! ?, M encode);
: ~6 ]+ o6 j4 O* {* `' c+ `4 o4 v }" w$ Q- R# V( o# s
}
" X+ t& H! B& k( { isNotEncode = false;6 P! y4 N6 n9 M+ Z. f. Z
}+ x2 N3 i' w# h/ q0 k) j) h+ z6 C5 w
return map;3 @9 R' ~$ B. [4 m5 } S8 S
} else {0 v* O4 j( V3 c: ^9 y. w$ r5 U
return request.getParameterMap();% M/ X) x6 U+ D
}. N, M+ n b$ ~! r; D
} catch (Exception e) {# s& }4 d1 X. L1 h! X! d" {: K
e.printStackTrace();
* o- B" T# q% y throw new RuntimeException(e);4 K! q3 l; O1 [6 l( ~1 U
}
1 J; j# `7 M' z) Z7 H$ O }" ?# Z! p$ O& `. `, z/ n( B
9 p- h. E, H, p3 v0 l# }) q
@Override
) U0 Y: H# j6 W- C* m public String[] getParameterValues(String name) {! i& P0 U4 r w+ R
return getParameterMap().get(name);$ m+ h0 C6 C) i( B! K3 }; t$ Q& N
} O2 d3 e4 y) B3 R
' {- H% |2 [# ]/ C: ~" [: B' N
@Override
9 R% s% k: u1 C7 U public String getParameter(String name) {' b7 h' V4 I. L! }
return getParameterValues(name) == null ? null
" x/ [8 z; D5 D. F# s : getParameterValues(name)[0];; u/ R; d& f2 A& n2 _1 ]1 W3 w
}" x; c) G+ |( e3 R" o, u
: e* e, ~+ m+ i
}2 [$ b$ u0 n1 O( n9 C$ E
}2 |" \( _( {& M# ` S3 W
& p8 J* I+ d& F2 W% u6 m1 F' E, ^8 w
|
|