当前页面: 开发资料首页 → J2EE 专题 → 过滤器问题,求救!
过滤器问题,求救!
摘要: 过滤器问题,求救!
过滤器如下:
package com.sinodata.lovecard.filter;
import java.io.IOException;
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.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
/**
* Example filter that sets the character encoding to be used in parsing the
* incoming request
*/
public class SetCharacterEncodingFilter
extends HttpServlet
implements Filter {
private FilterConfig filterConfig;
private String targetEncoding = "ASCII";
/**
* Called by the web container to indicate to a filter that it is being placed
* into service.
*
* @param filterConfig FilterConfig
* @throws ServletException
* @todo Implement this javax.servlet.Filter method
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.targetEncoding = filterConfig.getInitParameter("encoding");
System.out.println("这是什么编码:"+this.targetEncoding);
}
/**
* The
doFilter
method of the Filter is called by the container
* each time a request/response pair is passed through the chain due to a
* client request for a resource at the end of the chain.
*
* @param request ServletRequest
* @param response ServletResponse
* @param chain FilterChain
* @throws IOException
* @throws ServletException
* @todo Implement this javax.servlet.Filter method
*/
public void doFilter(ServletRequest srequest, ServletResponse sresponse,
FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest request = (HttpServletRequest) srequest;
request.setCharacterEncoding(targetEncoding);
chain.doFilter(srequest, sresponse);
}
catch (ServletException sx) {
System.out.println("HOHO");
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
/**
* Called by the web container to indicate to a filter that it is being taken
* out of service.
*
* @todo Implement this javax.servlet.Filter method
*/
public void destroy() {
filterConfig = null;
targetEncoding = null;
}
}
WEB.XML配置如下:
Set Character Encoding
com.sinodata.lovecard.filter.SetCharacterEncodingFilter
encoding
UTF-8
可是我的过滤器并没有执行起来,为什么呀?
缺少了
Set Character Encoding
/*