创建 Filter(过滤器)
实际开发中都需要开发一些很有用的过滤器,来解决中文表单提交问题啊,给请求和响
应加入 GZIP压缩功能啊,用户权限控制啊,等等,然而遗憾的 MyEclipse不支持直接创建
过滤器。在这里只好手工创建一个解决 Tomcat表单提交中文问题的过滤器。
选择菜单 File > New > Class,来创建一个名为 TomcatFormFilter 的类,包名为
filters。然后把类的代码修改为如下所示:
package filters;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class TomcatFormFilter implements Filter {
/**
* Request.java
* 对 HttpServletRequestWrapper 进行扩充, 不影响原来的功能并能提供所
有的 HttpServletRequest
* 接口中的功能. 它可以统一的对 Tomcat 默认设置下的中文问题进行解决而只
需要用新的 Request 对象替换页面中的
* request 对象即可.
*/
class Request extends HttpServletRequestWrapper
{
public Request(HttpServletRequest request) {
super(request);
}
/**
* 转换由表单读取的数据的内码.
* 从 ISO 字符转到 GBK.
*/
public String toChi(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes, "GBK");
}
catch (Exception ex) {
}
return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
private HttpServletRequest getHttpServletRequest()
{
return (HttpServletRequest)super.getRequest();
}
/**
* 读取参数 -- 修正了中文问题.
*/
public String getParameter(String name)
{
return
toChi(getHttpServletRequest().getParameter(name));
}
/**
* 读取参数列表 - 修正了中文问题.
*/
public String[] getParameterValues(String name)
{
String values[] =
getHttpServletRequest().getParameterValues(name);
if (values != null) {
for (int i = 0; i < values.length; i++) {
values[i] = toChi(values[i]);
}
}
return values;
}
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse
response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest)request;
if(httpreq.getMethod().equals("POST")) {
request.setCharacterEncoding("GBK");
} else {
request = new Request(httpreq);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws
ServletException {
}
}
清单 8.5 过滤器代码
然后修改 web.xml 加入 Servlet 定义,修改后的代码清单如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>
This is the description of my J2EE component
</description>
<display-name>
This is the display name of my J2EE component
</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlets.LoginServlet</servlet-class>
</servlet>
<filter>
<filter-name>TomcatFormFilter</filter-name>
<filter-class>filters.TomcatFormFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TomcatFormFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.aspx</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
清单 8.6加入了过滤器的 web.xml 内容
清单中的粗斜体部分就是新加入的过滤器的映射信息。