该用户从未签到
|
package cn.onecool.filter;& h" f' p/ c9 D
, w7 u4 e1 A/ s* h4 y# v( ?
import java.io.IOException;5 Y9 U* r0 z! \' c
import java.util.Map;
5 }9 B1 b" {9 X5 V) D- x& }
5 }7 c% n/ D% J) ^import javax.servlet.Filter;$ b& C& L' K/ e
import javax.servlet.FilterChain;
: g) c0 N0 E- k" Y( V" l$ |1 kimport javax.servlet.FilterConfig;
& E/ w5 l+ g/ b+ w9 P3 zimport javax.servlet.ServletContext;5 S6 u7 Y/ d1 }' ^
import javax.servlet.ServletException;% Q9 t& Q6 L2 T7 {5 n0 e
import javax.servlet.ServletRequest;% _/ d0 ?: N% J# [5 _6 a
import javax.servlet.ServletResponse;
1 S& ]+ h5 k5 rimport javax.servlet.http.HttpServletRequest;2 V: f' ~4 I: _- l: ^, @. ~! d( _
import javax.servlet.http.HttpServletRequestWrapper;
. ]) t9 x: ^: b4 y
# t, z x% H6 i2 B$ D: x( ? ipublic class EncodingFilte implements Filter {% O: O$ `- U0 _. d
private FilterConfig config = null;
8 `2 C% J" j$ _! l' }* E private ServletContext context = null;
- D. C; E) x( R- E/ m% n private String encode = null;
6 z% \9 h2 N* z4 Z5 ^! P. x3 T, J
4 J5 [, R! c" i8 A/ K8 ` public void init(FilterConfig filterConfig) throws ServletException {
% f; N3 s0 w: M* w/ F this.config = filterConfig;
]) G" w8 {5 ? this.context = filterConfig.getServletContext();
' r) e5 ]; M+ `* S! w- p$ S- d% w this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");
( [" u2 `" }6 f. h0 m }
7 t2 z5 F, q) I+ H2 F, }; {4 x' b2 o* ~; q1 C7 y
public void doFilter(ServletRequest request, ServletResponse response,2 m% L$ _5 T1 a( O1 e
FilterChain chain) throws IOException, ServletException {
X% B, a) |, b# B' i, J5 @ R! u7 i1 t/ t d+ p" W) ^, o
// --响应乱码解决" d8 g3 u& a* W( V; U
response.setCharacterEncoding(encode);- h2 \ {: d3 J! `9 a
response.setContentType("text/html;charset=" + encode);9 u2 s7 [" [# U
// --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题. T l8 n6 ]6 E
chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),& a5 t& K4 s$ I [3 c
response);/ B8 I' g1 Q7 E3 ~) j8 l. m
}+ Y6 V2 S% f; Y+ M
# T! g# f4 l% U9 T6 }6 {
public void destroy() {1 f, G2 y( G4 D
. x6 }& }" @2 Z5 k" x2 X, N }4 R) M2 _) t, l0 f" X+ D
# O9 j9 C6 |$ u
private class MyHttpServletRequest extends HttpServletRequestWrapper {/ K2 _' U! y }; S" y
private HttpServletRequest request = null;: z& N# n% m! U V, {0 Y
private boolean isNotEncode = true;' o, e( o A% _; o/ k" H4 Z* v
: p; w: P" U k" ] public MyHttpServletRequest(HttpServletRequest request) {* L+ H) K; q4 K1 x' X
super(request);9 k2 X9 m# o; s4 j6 d
this.request = request;
0 b& {2 Z. j9 S3 l: w* S, u! b ` }, K8 v3 V9 Z' M+ j K) E' {
5 B) r2 s: d9 d+ M1 U9 } @Override
`9 l* y- @0 C4 p9 x9 A9 G7 M public Map<String, String[]> getParameterMap() {
- W t0 K" I" r) l) d/ z B& n0 r try {! D% }/ P* g/ v3 u3 Q
if (request.getMethod().equalsIgnoreCase("POST")) {
6 I+ Y8 e. N2 x+ D0 V! Q; l$ { request.setCharacterEncoding(encode);
5 z/ |, e% Z6 A* M" I5 `' O return request.getParameterMap();
$ l) S( `; [( g1 m/ d# V } else if (request.getMethod().equalsIgnoreCase("GET")) {/ U6 j) X/ h& y2 N- t
Map<String, String[]> map = request.getParameterMap();
3 E3 J: c, @( }: X; K1 t$ t if (isNotEncode) {6 `1 _ G+ ]* C. L8 H# U$ }
for (Map.Entry<String, String[]> entry : map.entrySet()) {
& j# C h3 f- q: l+ V String[] vs = entry.getValue();
& w0 I% W& Q+ N) d# i for (int i = 0; i < vs.length; i++) {
6 ~) Q/ D& L9 F+ `( d- U vs[i] = new String(vs[i].getBytes("iso8859-1"),
P# ?# Z; r" `% C% V4 K( y" V encode);
0 t* |( @- K, P( u0 O% o- ? }# }, O8 a4 u% {, m
}
2 A/ u- Y! |3 {; j1 A$ a" P( d isNotEncode = false; z; L' {5 p+ i" ]% D: J
}
3 J/ x/ L( D; w2 _ return map;
% E; P( z1 {, F, A5 F- R) }6 F } else { @( ^" l3 k% e- Y: q
return request.getParameterMap();2 G+ e: f' s* C t
}
% A$ K s" Z- i8 C# T8 J } catch (Exception e) {2 q7 Z0 b2 Q- g8 ^5 X( m, `" ^
e.printStackTrace();0 P/ R2 m3 C0 U b% J
throw new RuntimeException(e);3 E: H8 c+ |' W t; R$ H A
}3 o- Z5 g! s& G6 m' Z" L
}3 u# o9 I- R8 C& Q8 e- m( m
/ a+ Z$ k' L5 `0 m4 L @Override& a' y- @! \: I% d, _
public String[] getParameterValues(String name) {
8 C# p: R7 T6 A2 [ return getParameterMap().get(name);$ w9 e1 G! O: ]# L! l, ~
}
: L$ n% w. \ H9 q! ?+ C5 S
, I+ A) Q& p+ Y2 t8 t @Override0 l, F `4 ?, q3 z
public String getParameter(String name) {
# ~$ _3 L" G0 E- L# v3 G return getParameterValues(name) == null ? null
/ ]$ Q- r r, h0 _7 R o& L : getParameterValues(name)[0];
" v7 C' [/ ^/ n3 g8 A: X } b) H* f# K( Y" m! G
7 y; W; z/ v+ y" h" A. M1 v& A* r
}
, Q3 H/ @( |( H4 \. b}; z v. ~7 ]& z; u. |4 V- h: \
! i7 o) {% i+ }8 P3 a1 k, q6 q; J3 T
* U9 F, ?* S7 D. w" m. C& {! _ |
|