package com.itheima.tfy.filter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class AllCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null)
encoding = "UTF-8";
// POST请求中文参数
request.setCharacterEncoding(encoding);
//字符流或字节流输出的中文乱码
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
MyHttpServletRequest mrequest = new MyHttpServletRequest(request);
chain.doFilter(mrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
class MyHttpServletRequest extends HttpServletRequestWrapper{
// public HttpServletRequest request;
// public MyHttpServletRequest(HttpServletRequest request){
// super(request);
// this.request = request;
// }
public MyHttpServletRequest(HttpServletRequest request) {
super(request);
}
//只针对请求方式是get的请求参数进行编码
public String getParameter(String name) {
String value = super.getParameter(name);
if(value==null)
return null;
//get方式
String method = super.getMethod();
if("get".equalsIgnoreCase(method)){
try {
value = new String(value.getBytes("ISO-8859-1"),getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class AllCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
String encoding = filterConfig.getInitParameter("encoding");
if(encoding==null)
encoding = "UTF-8";
// POST请求中文参数
request.setCharacterEncoding(encoding);
//字符流或字节流输出的中文乱码
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+encoding);
MyHttpServletRequest mrequest = new MyHttpServletRequest(request);
chain.doFilter(mrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
class MyHttpServletRequest extends HttpServletRequestWrapper{
// public HttpServletRequest request;
// public MyHttpServletRequest(HttpServletRequest request){
// super(request);
// this.request = request;
// }
public MyHttpServletRequest(HttpServletRequest request) {
super(request);
}
//只针对请求方式是get的请求参数进行编码
public String getParameter(String name) {
String value = super.getParameter(name);
if(value==null)
return null;
//get方式
String method = super.getMethod();
if("get".equalsIgnoreCase(method)){
try {
value = new String(value.getBytes("ISO-8859-1"),getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
}