该用户从未签到
|
package cn.onecool.filter;2 x3 J; ]9 H! U2 [& S9 ]
5 V" P5 J6 P( m7 }" ]
import java.io.IOException;/ c/ `5 Y$ ]/ I5 w* ?: N/ Z" |7 x
import java.util.Map;
8 p( n7 h' x: s8 d3 _3 R0 n. N
: _' d" `. T. X1 p" ~) q) Rimport javax.servlet.Filter;4 ]! e3 p" ?: b1 S
import javax.servlet.FilterChain;" f1 p% x( X* b
import javax.servlet.FilterConfig;/ e9 `& o l% @: Q6 e/ _
import javax.servlet.ServletContext;+ s9 f! i7 d5 D, V# R5 Z
import javax.servlet.ServletException;* X/ d" O9 E' v
import javax.servlet.ServletRequest;
! z5 Q |$ i+ iimport javax.servlet.ServletResponse;7 P# Z, k4 G& e5 m: L2 |
import javax.servlet.http.HttpServletRequest;+ h9 U4 D: J; C& {) ^- ]
import javax.servlet.http.HttpServletRequestWrapper;
, B& _1 Q" p( I% f/ f& K% s+ s. P( f( o1 M* w% e- ?: Y
public class EncodingFilte implements Filter {
2 ^% |' i0 w" _/ I }* ` private FilterConfig config = null;! Z* {$ s* t! }
private ServletContext context = null;. b3 Q3 W0 x+ P" \
private String encode = null;
8 w# M. B) X( E) I) D" W
0 C% m7 {# @% w+ \% Z1 r public void init(FilterConfig filterConfig) throws ServletException {& z7 E3 G1 F3 b/ g
this.config = filterConfig;( S; M4 l6 ?% k z; @' R# ?
this.context = filterConfig.getServletContext();# N3 B+ ]. |3 n1 T! N* k* r, d9 h+ @
this.encode = context.getInitParameter("encoding")==null? "utf-8" : context.getInitParameter("encoding");5 X2 c$ j4 b# A5 L2 I- v
}% V; C% l3 N( w8 b1 O
. g* r9 q8 M3 g- w C4 t3 Z9 s! i# c
public void doFilter(ServletRequest request, ServletResponse response,, S5 l b" h q7 D8 @
FilterChain chain) throws IOException, ServletException {/ r$ @& A ~9 W
9 b0 Z2 ?9 \" \% m1 e) f* j* R // --响应乱码解决
/ D0 k# [ w% b0 K# N8 N8 O response.setCharacterEncoding(encode);
/ \' S6 q* F3 ^, L ` response.setContentType("text/html;charset=" + encode);
$ T5 @9 K3 t( B& n. f, ? // --利用装饰设计模式改变request对象和获取请求参数相关的方法,从而解决请求参数乱码问题
9 }" K9 C/ t" \$ b chain.doFilter(new MyHttpServletRequest((HttpServletRequest) request),
) T0 K8 `9 H8 \0 @7 ]* M' O5 D response);6 {* r0 L# v7 y! ?# {/ s7 C
}
/ U* ^' R0 W* w0 Z5 q" T/ g- A$ m, {* E u
public void destroy() {
, a# K! Q9 a# x8 |& c8 P" c- }5 H, X, [! A6 N
}
; H, W. V y) m- |% H0 O7 ?1 Z( b
private class MyHttpServletRequest extends HttpServletRequestWrapper {8 X' W/ t [0 N5 p! K) p
private HttpServletRequest request = null;
# W3 ?0 U5 W# a! h3 ] private boolean isNotEncode = true;! }7 i! a9 {: i5 W
* w! A: q8 j# I public MyHttpServletRequest(HttpServletRequest request) {
- e5 W" L4 m+ N super(request);
! T X3 |2 z* H6 T" X this.request = request;
) I" u8 m% o. [; D$ J2 ?4 I5 | }
& Y2 \3 B( ]& G4 H8 F" X6 g9 ?
) R# V. ^! W$ O* I/ U @Override1 ?& \2 ], B8 Z2 h8 ~0 t
public Map<String, String[]> getParameterMap() {7 p- @" b5 {4 x! l% ?, P
try {
- G# {7 R; _2 l6 K+ o9 J) u if (request.getMethod().equalsIgnoreCase("POST")) {
; ^: \' M* t5 p; ~ request.setCharacterEncoding(encode);
6 u% N; m, F- F% Y* ?' z return request.getParameterMap();% O! a' M9 d) {5 ~: f! f
} else if (request.getMethod().equalsIgnoreCase("GET")) {
- L) X/ N. }) D8 k1 F2 C Map<String, String[]> map = request.getParameterMap();
- ]/ X6 r# b) [5 y if (isNotEncode) {. M- }( Q$ U6 J8 d9 ]
for (Map.Entry<String, String[]> entry : map.entrySet()) {/ F! b, v3 d4 E! k9 D4 h
String[] vs = entry.getValue();9 s3 {7 O( u8 l- l
for (int i = 0; i < vs.length; i++) {
& g3 h4 R* {8 J& t4 j vs[i] = new String(vs[i].getBytes("iso8859-1"),
* L4 v4 }3 H% n encode);
$ N' x8 {5 }, x; @$ w }
1 S; P ]( [% Y2 i% Q }) h+ u+ j( b- P1 S& c- O. S
isNotEncode = false;
5 C( R6 S. Y( @: {! p8 P# r }6 Y2 d7 `7 {0 u9 b" J
return map;
& U) t/ x$ @) W4 l8 D } else {
' m7 x- J; w- I& B" { return request.getParameterMap();
) ]+ `; g& H! b( d+ a% B1 Y }$ n2 q5 m4 ^% K6 l) [
} catch (Exception e) {
' T* L7 S+ V) F. O) q9 e e.printStackTrace();
8 K" x6 O; w( r& K+ ^; @3 \) L0 }! D4 ` throw new RuntimeException(e);( [4 s1 q5 D! o8 V1 W! u7 P
}* i% Z( B' S" d$ H+ {( Z" L' ~
}; x& o# R' G3 G% r5 L
: I8 P( v% ^* v+ X9 J8 q2 y( r
@Override
6 {6 p! I5 |6 X" x: C- i7 b public String[] getParameterValues(String name) {$ r5 }& l" E; l. T
return getParameterMap().get(name);
% o. y' O# |1 L# j* \, O8 A; P, g }
6 X1 i. u$ B/ `% I0 U @ \
4 O2 ?) `: ~* h( {) x @Override* z4 W$ A0 X+ b8 g# C1 V" x. g
public String getParameter(String name) {& ]: r5 p6 h' D& N) f- M7 W( H
return getParameterValues(name) == null ? null
$ T( r1 `$ V% t : getParameterValues(name)[0];
( i' F1 n) x2 Q! d1 q7 {$ u+ N* T; N }
% Q( L( E2 g' t, J- w& ]8 ~
3 v6 R) a5 ]0 n# v }6 x x7 \2 ^/ c- i: \
}3 n% i2 X9 z* ?$ U* A
6 q7 p: I, `6 G3 t* e. m7 d8 i7 y" S7 F/ h! {
|
|