一、創建一個抽象類httpfilter實現filter接口html
裏面實現獲取FilterConfig()和 getServletContext()方法;java
建一個dofilter(HttpServletRequest request,HttpServletResponse response,FilterChain chainr)抽象方法讓子類去實現;web
提供一個init()初始化方法能夠讓子類去重寫;app
package webcard.filter;ide
import java.io.IOException;post
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;this
public abstract class HttpFilter implements Filter {
private FilterConfig fconfig;
@Override
public void destroy() {
// TODO Auto-generated method stub編碼
}
public FilterConfig getFilterConfig(){
return this.fconfig;
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {url
HttpServletRequest req=(HttpServletRequest) arg0;
HttpServletResponse res=(HttpServletResponse) arg1;
doFilter(req, res, arg2);
}
public abstract void doFilter(HttpServletRequest request,HttpServletResponse response,FilterChain chain)throws IOException, ServletException ;
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.fconfig=arg0;
init();
}
public void init()throws ServletException{}
public ServletContext getServletContext(){
return fconfig.getServletContext();
}
}spa
二、提供一個myrequest繼承request,自帶解決get編碼方式
重寫getparameter()方法
package webcard.filter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class MyRequest extends HttpServletRequestWrapper {
private String charset;
public MyRequest(HttpServletRequest request,String charset) {
super(request);
this.charset=charset;
// TODO Auto-generated constructor stub
}
@Override
public String getParameter(String name) {
HttpServletRequest request =(HttpServletRequest) getRequest();
String value=request.getParameter(name);
String method = this.getMethod();
if (method.equalsIgnoreCase("post")) {
try {
request.setCharacterEncoding(charset);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if (method.equalsIgnoreCase("get") && value!=null) {
try {
String param=new String(super.getParameter(name).getBytes("iso8859-1"),charset);
return param;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.getParameter(name);
}
}
三、最後一步設置一個encoderfilter類繼承httpfilter類;
package webcard.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EncodeFilter extends HttpFilter{
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
//全站字符編碼
String charset = getFilterConfig().getInitParameter("charset");
// System.out.println(charset);
if (charset==null) {
charset="utf-8";
}
response.setCharacterEncoding(charset);
response.setContentType("text/html;charset="+charset);
MyRequest myRequest = new MyRequest(request, charset);
myRequest.setCharacterEncoding(charset);
chain.doFilter(myRequest, response);
}
}
固然最後別忘了設置context.xml文件中的過濾器的做用範圍:
<filter>
<filter-name>fil</filter-name>
<filter-class>webcard.filter.EncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>fil</filter-name>
<url-pattern>/*</url-pattern>//默認只過濾全部的重定向請求
<dispatcher>REQUEST</dispatcher>//過濾轉發 <dispatcher>FORWARD</dispatcher>//過濾網頁轉發 <dispatcher>INCLUDE</dispatcher>//過濾include </filter-mapping>