该用户从未签到
|
package cn.onecool.filter;
! c, H5 {6 j3 t# e2 R! \
0 C2 F. A( G& T9 H) n) ]) Bimport java.io.IOException;
* c( r! i" ^ g1 @% Bimport java.util.Map;
+ V: }: k+ X) [+ |7 Z, N5 i* B8 `* ~! O4 d9 c {) Y/ j
import javax.servlet.Filter;
4 p& l! f5 ~- f- Oimport javax.servlet.FilterChain;
) V, P/ O; _, n2 `8 Y6 t+ ]; gimport javax.servlet.FilterConfig;& P* D; i# a; |' y2 F: A
import javax.servlet.ServletContext;* y2 ?; h1 I( d6 e
import javax.servlet.ServletException;
% C; l8 W5 p6 ?import javax.servlet.ServletRequest;
9 |9 }7 ^- w0 F1 S7 dimport javax.servlet.ServletResponse;" ]' r$ N' R4 M) j& D( R
import javax.servlet.http.HttpServletRequest;
/ s0 n0 c0 t$ w. r7 D+ D1 h" \import javax.servlet.http.HttpServletRequestWrapper;% V; v( v+ h0 R# i- g
( Q& t# [$ p% D) y( upublic class EncodingFilte implements Filter {/ |: r/ S- S' T3 n
private FilterConfig config = null;
, N6 B/ `) ?2 F; W" n% e6 k& E! D private ServletContext context = null;
" u+ z+ r, V0 `9 G y private String encode = null;
$ \* }; W( l6 K
, p" q9 f3 c6 v! c3 X) L public void init(FilterConfig filterConfig) throws ServletException {
; [. G# R3 g) H0 `9 x G this.config = filterConfig;" d* X) p% U9 Z4 l3 D
this.context = filterConfig.getServletContext();8 q) `3 Q7 j7 o2 I7 `: S
this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");
) Q" n& P( U& V* x- b7 y. O }; l1 h/ M& I/ l! U: ^; k
$ J; f1 p' } ?: \4 Q! \$ A
public void doFilter(ServletRequest request, ServletResponse response,
' j6 D& e! R& h% Y+ x3 W FilterChain chain) throws IOException, ServletException {$ |9 @! u# ~7 G U! ]% k
. O& v0 r- j) j // --响应乱码解决
2 x2 V- k4 ]2 `4 \ | response.setCharacterEncoding(encode);/ T1 p+ t- P$ z; M2 y# q
response.setContentType("text/html;charset=" + encode);
5 H0 N) W8 h1 n7 C6 m, Z6 l // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题7 a+ g' l$ O0 q) s
chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),, \2 X' u0 i2 {, V
response);' g' `- m# ]$ Q
}
, V9 A0 t$ Z ?6 M
/ g7 {! X1 E+ M( Q: p. d public void destroy() {
j2 \: n% z7 y$ Z3 M# H; t" R, _- l' U# z
}
2 m' b$ w$ M5 M8 _8 [
. k) J m) W* |$ M L& U' i+ _$ Q private class MyHttpServletRequest extends HttpServletRequestWrapper {, r1 K+ T' e m/ {3 Q; G
private HttpServletRequest request = null;
* p7 {+ C: Q6 b! r private boolean isNotEncode = true;) _0 A T2 o, L
1 v. M7 N) j( W1 D
public MyHttpServletRequest(HttpServletRequest request) {; u$ o! E9 |0 N7 R
super(request);
& n7 }5 i) y; Q$ {8 v this.request = request;
6 X% Z! h) y3 n, v! ?/ y }
3 |$ a( D. y4 a( p3 R
- V/ c0 Q. E. A8 ?) P2 W @Override7 Q) @. C, \+ z( P/ G, ?
public Map<String, String[]> getParameterMap() {/ N8 C2 S( I! j' ]/ ?2 U! s6 [
try {7 d8 p# |1 K P0 W z! ` u& @* Y4 h
if (request.getMethod().equalsIgnoreCase("POST")) {
" ` c8 s V7 `! h2 Z request.setCharacterEncoding(encode);
9 C& L/ S- F( y, ~8 [; v return request.getParameterMap();
* w- E* B9 W# o/ R$ m7 J } else if (request.getMethod().equalsIgnoreCase("GET")) {+ u$ ?/ g& f+ @9 K
Map<String, String[]> map = request.getParameterMap();
& D6 }! \' h% [ if (isNotEncode) {
- a! `& d+ x" h+ F- d! T1 |; K for (Map.Entry<String, String[]> entry : map.entrySet()) {2 K8 O- E5 g# a$ V3 K
String[] vs = entry.getValue();
" T7 x w$ k# F# z! o, ]' Z! s for (int i = 0; i < vs.length; i++) {0 W$ j1 Q Q# P1 @: p
vs[i] = new String(vs[i].getBytes("iso8859-1"),2 u6 R+ o/ O. m. x) [
encode);
/ ^+ Y. g- e) i ~' ~. w }
2 N6 i7 _* l2 ]7 C9 s& x! ~6 E' l }' n, I* X U( Z1 F# X8 k) n
isNotEncode = false;
: A5 t8 A, X# I5 P }
6 j+ [* Y! B5 c4 i/ v: a' N( \, T return map;
/ D% c Y0 T( O9 P( Z } else {
7 E2 V; G, w7 Z9 q return request.getParameterMap();9 H9 Y, a# Q+ y. Y0 G# k9 s
}% T: t. v+ \ A% g( l s
} catch (Exception e) {
1 A# f; G; _; ^ e.printStackTrace();
+ l5 u1 d d# q! J; m2 L3 N throw new RuntimeException(e);
; {% m' h% D/ q( v& s, g }
/ n! u& K* ?0 h: w8 L }7 M0 w/ e r6 t
' K( B: Y1 j Y: Z0 S @Override9 V$ n: E: p7 S. c- P+ b- O$ d
public String[] getParameterValues(String name) {! g* x7 T% z4 q% k
return getParameterMap().get(name);2 W( _3 c1 u- c' k2 H
}
2 U' c ~% }1 X0 m
; p/ g& o/ ~0 m @Override
: H# V E3 R. y6 W0 n public String getParameter(String name) {; h5 b# g( x( E* O, e
return getParameterValues(name) == null ? null
3 A; m1 v! g( k* \ : getParameterValues(name)[0];
1 U% e* D& Q; M* w3 p2 I }$ \5 W8 ]3 W& a, c$ F* y
W! b+ r6 ?! K$ m }9 m& q; o1 D' `$ C [9 K8 T7 a
}
0 q$ m# p- u' ^. Q
& ]5 M; e9 B/ N' A5 h
8 X+ I1 ]1 V+ C |
|