该用户从未签到
|
package cn.onecool.filter;
9 u+ b2 v! z- B5 O) x4 B
+ @' G8 }7 W' K6 r/ Timport java.io.IOException;% n" ^/ M# F% @& @
import java.util.Map;
% G8 B6 {2 z( s6 [3 y& |. q+ B7 s
u$ C( |3 }2 y' M! Fimport javax.servlet.Filter;
* J; e2 W2 S0 M4 `1 {9 Jimport javax.servlet.FilterChain;
- H) D! o: \7 z4 G% Nimport javax.servlet.FilterConfig;% b; w# M$ k% W( |
import javax.servlet.ServletContext;
: u, n. Y+ Q# Q; _* [import javax.servlet.ServletException;6 x4 J6 n. `" r6 L
import javax.servlet.ServletRequest;$ |8 Y: u( a/ z1 ?3 y
import javax.servlet.ServletResponse;
* K) V# ]9 l" Wimport javax.servlet.http.HttpServletRequest;: S4 @+ U Z& o. v
import javax.servlet.http.HttpServletRequestWrapper;/ }' o1 @. Z9 Q* H* C( s
7 A0 y7 N; g' h1 D }4 Opublic class EncodingFilte implements Filter {( x) N% h( r2 ^8 [; M/ h5 @6 d A
private FilterConfig config = null; E: {" J$ O2 f" n+ Z% L
private ServletContext context = null;
a0 p/ I1 A8 i; e private String encode = null;: U+ i* R2 K1 m) d) M5 }: [
) @+ @# Z. y5 t; a
public void init(FilterConfig filterConfig) throws ServletException {
0 F: [: m' ~4 R3 D; o# O3 p this.config = filterConfig;+ \: L: j, i+ E1 `/ q
this.context = filterConfig.getServletContext();
1 {6 b% y ^1 \ this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");$ {; R; C; v; N& x2 q5 [
}
- ]# h- J1 c) Y/ X+ A0 l5 C9 Z4 [( n. h0 Y1 ?4 M% i( b7 U$ i# _6 R
public void doFilter(ServletRequest request, ServletResponse response,
: x. o: Z# Q! b6 v, x7 h- Y FilterChain chain) throws IOException, ServletException {
( d- b% @3 m/ ~* N2 G y1 n- s2 a$ i) p' s" W# q
// --响应乱码解决. \1 i8 t7 x! m9 p
response.setCharacterEncoding(encode);3 }8 U0 m4 G f6 D( \7 M
response.setContentType("text/html;charset=" + encode);
3 [+ B/ Z0 N- g* i q // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
& M2 R. T% N2 |/ Y* C- s3 k9 _ chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),
$ G! Y$ L9 S- [- k' D9 p2 Z' W$ \5 v response);4 n0 P- a5 F! t9 R' y p
}/ d& K6 t' p9 ?
) K7 r7 t; v6 _' ?4 n3 v3 d
public void destroy() {' h5 @! K5 b: x2 {4 ]9 j8 e
) v, _9 _# y5 c C/ m5 w+ M }
% E, @" b! p" K
5 |; g0 C' Y0 U0 i private class MyHttpServletRequest extends HttpServletRequestWrapper {/ o! j6 x8 i% U- Y8 E: ?9 W9 y3 Q/ |
private HttpServletRequest request = null;
% a& ^4 K9 n, X private boolean isNotEncode = true;
: z, C' B( A/ i* Z4 s! S- t2 f9 |6 V8 x8 L( I6 }! I
public MyHttpServletRequest(HttpServletRequest request) {
4 Y* R" Z5 r1 U! F* F+ j3 V) U9 S super(request);
. u/ e% @# s& {# k" t; W8 D this.request = request;; ^; I# }: ^' U
}4 X* d: {) R# G. \- h: [$ i
3 e( S- U$ \7 }* m
@Override
! T$ s7 Q* O. I } public Map<String, String[]> getParameterMap() {
) F2 ]4 F M4 D! L try {
* J. G5 p' ^" M9 X- m if (request.getMethod().equalsIgnoreCase("POST")) {
+ Y& C M7 D; C6 \" W# s request.setCharacterEncoding(encode);
' m8 h. i6 j) [1 A( V- { return request.getParameterMap();
1 U0 S6 P! b* H } else if (request.getMethod().equalsIgnoreCase("GET")) {
5 K8 t+ j) z9 l6 m; w Map<String, String[]> map = request.getParameterMap();* n$ b9 \/ f) c- p
if (isNotEncode) {
& P( f. u! O7 w9 f5 M, c9 \! v8 q for (Map.Entry<String, String[]> entry : map.entrySet()) {, \1 U: [2 _) X# c
String[] vs = entry.getValue();+ m1 C6 B3 {6 }' I
for (int i = 0; i < vs.length; i++) {
# c, b4 m6 r0 D' x vs[i] = new String(vs[i].getBytes("iso8859-1"),; h% G) T4 A3 ^- w S
encode);
G0 G, j, f& t }6 L. N& g$ K$ u! ^9 {
}' D' }0 P9 X( Z
isNotEncode = false;( e# `" J9 F% q
}
9 E" j$ {3 p t return map;6 F9 P- G" i2 C ]& [9 T, k3 Y
} else {+ I- h$ m- f5 e, R, F( q& X
return request.getParameterMap();$ U5 e' n: R5 V y: B
}
T2 U9 |( V3 L1 S } catch (Exception e) {
# Y6 W3 s4 M" Y z# W/ w: y6 e6 D e.printStackTrace();
* L/ g1 M/ @9 _, D throw new RuntimeException(e);
5 `2 G8 t. ]6 z; E5 r }
0 i3 y5 T$ F& e+ z" K4 w }
8 @8 A% }: n; g: T. C: _6 d' w' i
@Override. v" @% P* R" p9 Y9 k7 z. H/ r2 u! b- ~
public String[] getParameterValues(String name) {
( R/ L* I4 l2 I# B0 D. Y! q return getParameterMap().get(name);
v# W6 [& X/ H, A$ _ }! s0 _1 z$ c' ~
& m1 T" A, ?1 y5 x9 L1 M! s
@Override
, Z7 N/ i1 D9 b/ A: B" I4 O& { public String getParameter(String name) {
* z& O3 N' i7 z9 O6 X9 C& g return getParameterValues(name) == null ? null6 ]3 B; u7 o3 ~, [6 z
: getParameterValues(name)[0];) L2 ~- z5 b' Z1 S S+ y, V' i
}% n# g4 l! t3 F* d
: m( z0 ]! x# E, z/ b- R }
3 _: v* k* j4 f. T}
8 f4 F; P; Q8 E N+ Y8 A1 J' T% H7 R9 a3 p1 \
7 \/ N$ @& }3 A) }5 j& \3 I! N |
|