该用户从未签到
|
package cn.onecool.filter;
8 r! g- _( J) m; w* W8 l! h# \- n
import java.io.IOException;8 a$ Q: [& `, Q5 y# |0 r0 S$ X0 u
import java.util.Map;) U- t: f1 g: R0 i/ e* X. d
% J7 Y0 k k5 O1 ~" p. N* Y- Fimport javax.servlet.Filter;6 y8 ]7 m% c( `. Z# `8 G( U* x
import javax.servlet.FilterChain;
B% g3 @1 O& Q t7 g3 Z- o, _import javax.servlet.FilterConfig;
- x5 ~% h' L6 Y5 Cimport javax.servlet.ServletContext;
* r6 I2 E! e4 f0 B( O: F" Iimport javax.servlet.ServletException;
, N3 E/ k: y5 N, U% D8 b1 i7 Nimport javax.servlet.ServletRequest;& l9 R9 m! o, ]2 ~# I
import javax.servlet.ServletResponse;
) b$ y$ }) N4 G( G5 Simport javax.servlet.http.HttpServletRequest;& m1 J0 P* I, D+ y' A( l$ m% t
import javax.servlet.http.HttpServletRequestWrapper;
; m) k, z2 A$ s8 Q2 G0 l' o: t, W- g* Q" y1 G
public class EncodingFilte implements Filter {
. Y% G7 k) T2 u1 w private FilterConfig config = null;% p4 p$ D* g9 B$ l, J7 ~' d
private ServletContext context = null;
s2 w! k6 |+ O# n J private String encode = null;
0 U/ G' Z: E+ Y3 x, }+ F9 h: [+ T# j3 h) m1 b
public void init(FilterConfig filterConfig) throws ServletException {7 j( p5 b4 O, i. K4 @* O- |1 r
this.config = filterConfig;
1 r) U2 O4 [( [: G; z this.context = filterConfig.getServletContext();' y+ S$ |: C1 k$ g8 z1 q4 f
this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");) n2 I1 X) z4 {8 E. m" U
}
$ b2 D. ]) I. u9 n* x1 u2 k
- A: y; M8 ~6 Y# _" D6 E public void doFilter(ServletRequest request, ServletResponse response,
1 n4 I; I) z" L6 @ FilterChain chain) throws IOException, ServletException {
( f3 y9 V4 A) z& Z
2 _6 Y F4 H1 e ` // --响应乱码解决6 K: r P, {3 q
response.setCharacterEncoding(encode);
+ z4 J( N8 r, K+ l( Z, `! f response.setContentType("text/html;charset=" + encode);
) U( ^1 w+ ]" s3 [5 C // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
9 ]7 Y: c% S% M! d% N ~: F chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),
- C) U8 x. j0 { response);
6 \6 e4 U; n3 d6 I- d6 `& q }
# K5 n5 n% p8 j2 b; S1 d+ P8 G4 W
( g' H8 W. N$ W8 l3 P* m8 u public void destroy() {
) E4 }1 a0 z$ k3 n6 M b
% ~' J! G: x( j0 @3 g* L" R3 C }
/ A3 Y" x% {9 H1 |4 K% _
5 M7 B" [9 E, _- H9 `( j8 L private class MyHttpServletRequest extends HttpServletRequestWrapper {
$ E r" x; {# s private HttpServletRequest request = null;
" Y/ I6 d5 z8 H5 y+ C private boolean isNotEncode = true;. w4 i( B# b- P8 c0 ?- T1 m
- v4 Y' ^9 {+ q; H
public MyHttpServletRequest(HttpServletRequest request) {
# J; C) M2 D' S& [5 B% ?# ^ super(request);
0 u- L. g( }; v this.request = request;7 {, j2 Y( }6 F+ P' q: S8 c
}
# j, {, c' t* X5 e: \1 k0 O5 R$ V4 B+ A, j& y# ~
@Override
/ F5 r% `4 T4 F4 F3 j) Y5 h public Map<String, String[]> getParameterMap() {
V- K- D5 B+ u4 z7 o try {
+ {1 W. ?( j6 O2 g if (request.getMethod().equalsIgnoreCase("POST")) {3 @2 s* J& x/ i |1 e# _0 I
request.setCharacterEncoding(encode);# o( G9 G7 T q% F) Q
return request.getParameterMap();, j, Z% ~8 t3 w
} else if (request.getMethod().equalsIgnoreCase("GET")) {; g* Y% S, S8 V% f( `! m; H
Map<String, String[]> map = request.getParameterMap();; F' e& q8 b, I% H1 c$ l/ b$ ]( J. J! t
if (isNotEncode) {
0 _. i L" W( l W) b for (Map.Entry<String, String[]> entry : map.entrySet()) {$ N9 ?- m; S0 ^- ~0 D* z8 ]
String[] vs = entry.getValue();, l8 A! F9 W% ?, T
for (int i = 0; i < vs.length; i++) {0 q8 r3 y! m4 m2 W3 e* X
vs[i] = new String(vs[i].getBytes("iso8859-1"),
$ z: b2 N& V b9 @$ h4 D7 D encode);
4 h C$ A" I( o* N8 z2 s }5 e( U/ T7 f- t# C) l
}. N$ V6 u, `$ @0 z% u8 g2 D
isNotEncode = false;
1 [5 _0 t/ h3 y, k \) S, l) f }' B( N9 C1 i l4 x' u
return map;
3 I0 g2 ^/ I; W' U* S. m } else {
: ]* S: c4 L8 I0 D$ k# s return request.getParameterMap();! K, k+ _: `" W
}
+ \: W, A4 O$ e$ C } catch (Exception e) {2 H; @. y4 M0 } G. B
e.printStackTrace();# P: V% i/ R( V: w7 y) f
throw new RuntimeException(e);
- u9 B4 S# p* F1 q }, ?, E3 A( K! C- ^3 d5 v: p% K! I' l( Y
}2 }+ n. _* z* I6 q
8 o! S( K: A( a% O- k% j- J/ p @Override" K6 P l' a2 X N* q1 A' m1 y+ T
public String[] getParameterValues(String name) {0 |5 p; V2 r' D* v
return getParameterMap().get(name);
: S2 ]# h( F! k+ @ }5 C x# H7 u5 J$ T+ v" h# V
! b9 n1 p: N) u X/ L! ?7 i" R0 q$ @ @Override
- ^, p/ e) T6 f7 t3 ]$ q public String getParameter(String name) {
, y8 r+ l% O- `0 U3 t5 d return getParameterValues(name) == null ? null
o9 n# G: r# o, g9 g : getParameterValues(name)[0];
6 R" D4 ~: t9 x }/ h% O- m1 _( B$ ?
: Y* F. }5 J, Y3 b7 b3 _/ `$ G7 ~ }
3 ?4 `* c0 E; \& \( p# D6 D}0 {& r+ O* I: K6 Z- @
( Z9 p# q6 d- c) a1 x
5 v5 h- ~. r- P8 D |
|