该用户从未签到
|
package cn.onecool.filter;
}' ~) _7 _ r$ X
' u P' [$ G9 Y7 yimport java.io.IOException;& S- V& ]" ]. a4 c ]
import java.util.Map;
1 Q" B3 U" R$ n. I/ M
$ i0 i. {; {- p1 g5 A% j5 e3 A0 ^import javax.servlet.Filter;: | v# Q$ K# Z% T
import javax.servlet.FilterChain; _, |4 q) z1 Q/ ]
import javax.servlet.FilterConfig;
6 A- f# E6 t( F7 s, t: g7 `import javax.servlet.ServletContext;) n% \$ y `$ ~( Q3 R
import javax.servlet.ServletException;
, g. \8 r* ~4 D9 ]import javax.servlet.ServletRequest;" C: S7 K3 R+ G
import javax.servlet.ServletResponse;
8 i4 h+ M0 S1 L5 X- x7 Pimport javax.servlet.http.HttpServletRequest;
" o) s: E! H( k" Gimport javax.servlet.http.HttpServletRequestWrapper;
& {/ R4 G( K# t7 E m: w9 r2 D8 o2 I f
public class EncodingFilte implements Filter {
% E6 ?4 u# ?6 H) l _ private FilterConfig config = null;
# _. | l, M1 R1 q o private ServletContext context = null;5 Z8 l! _3 d4 Q+ r7 Y7 ^6 h4 ?1 W9 w
private String encode = null;
7 b( w2 |8 S% n4 F' i! \# M* s! I) B: J' m( h% |: u5 w
public void init(FilterConfig filterConfig) throws ServletException {2 k. P! X) G. p* |5 L7 ~
this.config = filterConfig;
) b3 J- r2 ~# f8 x this.context = filterConfig.getServletContext();1 Q) Y9 f% P0 Z n" A
this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");
; w4 X" g6 X9 w2 o* Z }; [: r5 E4 Y; w p/ m% t
' D8 O" e0 J, f; j/ F# S# I* Q. Q public void doFilter(ServletRequest request, ServletResponse response,& m1 O( m5 y8 G$ C: e, Y7 v+ d
FilterChain chain) throws IOException, ServletException {
5 G _- \; K6 c/ _: q. y% z; `- G6 i: `! i
// --响应乱码解决/ _- A6 e$ `& T$ w6 ^& _
response.setCharacterEncoding(encode);; D$ g$ r( m: w
response.setContentType("text/html;charset=" + encode);; K7 |. E, l9 `- Z8 x
// --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
+ [" k, B; N$ R3 v chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),' g- V3 L. N$ g8 ~
response);+ l. p: `0 Y8 G4 R- U" O
}
$ F( k6 m: q0 U; Z+ g
9 Z$ P0 V1 S- ^" W7 w public void destroy() {* G6 P7 w) a* B Z4 B4 ~/ e
2 k/ S1 T4 z) P; w }
9 I3 d* x% L/ O9 H0 T% O; u
2 k" Y# f$ P. m0 M; e) y private class MyHttpServletRequest extends HttpServletRequestWrapper {" J! g- W; C: n. ~ L6 N* I3 o
private HttpServletRequest request = null;
1 K" C6 t9 S1 P9 l1 ?) p! m4 _" Q& V7 |% c private boolean isNotEncode = true;
3 z/ P; S, c0 [0 O' v6 Q2 o: V" m4 s/ K1 ?* K N% ~9 Z
public MyHttpServletRequest(HttpServletRequest request) {
3 {+ M. D, O8 ]3 ]7 E super(request);
4 Y u; m) N5 L4 F- s this.request = request;& z0 a7 l1 s3 L9 E+ r. W
}
; y: {# @3 q* H$ n/ I4 c }/ S1 e. |$ L! Y, t
@Override
7 M. a+ X% ]* V' T+ A1 ? public Map<String, String[]> getParameterMap() {
L& L9 k( X; K- L8 Z try {5 v# ]2 X7 u" B( n& y4 x
if (request.getMethod().equalsIgnoreCase("POST")) {: w6 r# C/ [4 j0 y2 b" j/ y' O, z
request.setCharacterEncoding(encode);3 R8 @9 |1 h& e; t( {2 U. h. E
return request.getParameterMap();; ^1 @4 h3 i$ L' s/ z
} else if (request.getMethod().equalsIgnoreCase("GET")) {' B+ H! \& \; [4 j# u; v4 I, k7 o8 U. q
Map<String, String[]> map = request.getParameterMap();
% @3 D( r# m7 h2 F3 \ if (isNotEncode) {
5 G2 v4 T/ U3 B9 b for (Map.Entry<String, String[]> entry : map.entrySet()) {6 B! _8 b8 {% w$ v; i
String[] vs = entry.getValue();3 A6 t |/ f2 S
for (int i = 0; i < vs.length; i++) {
5 ~: L. |9 ^ H/ O4 S- @# I vs[i] = new String(vs[i].getBytes("iso8859-1"),
3 Z: c8 b. l: Z! R encode);! G8 A# N/ N& a" M" M7 n% N
}
- x. n% n9 c( }/ O }4 T$ }; W$ d8 V& z5 \
isNotEncode = false;- Z" d" p! r$ a
} G8 d1 c5 @: K' s7 Z" y
return map;& h0 l4 i9 U; \2 A
} else {8 [' o# ~3 M( }9 s; b
return request.getParameterMap();! i1 _) J" p8 v) t' t0 K, t
}
6 d, O2 T2 N s9 ]6 _9 c0 N; D8 g } catch (Exception e) {
/ b4 x4 l: ~' { e.printStackTrace();1 r' ` K4 x3 t/ [
throw new RuntimeException(e);+ _" g0 `" @ C
}0 R( M0 [# r4 F0 W" J9 q
}% z9 r' ^& b* ~. L6 S( a3 l: O
* h! t, [2 _, h* Q# \2 a% z
@Override9 z5 J' t% i2 P) v3 @6 k
public String[] getParameterValues(String name) {
& k% D* \) ?. G: S return getParameterMap().get(name);
9 A# w4 G) T. n4 _8 F- \* L" g }
: c% n* P7 f' \: M! M7 o6 c9 F+ o2 |
@Override, i! \% M1 a6 m/ ?0 V) N; P
public String getParameter(String name) {
m6 S! `: Z: i# A; H& s4 h return getParameterValues(name) == null ? null6 m! c; z8 _: q; ?( c8 q% |# m
: getParameterValues(name)[0];, x7 y3 x9 G! D$ s3 y' C
}, {" S- C D: B& J6 Z1 E
3 T% r. V J/ l+ t% M0 W }
- g4 G9 w$ x. U; a}; B. _: Y* S: Z
5 i& }6 M: i1 w# O( z) t/ f; T0 l6 h' x) f+ U4 t3 W5 o5 I
|
|