filter的意思简单的说就是过滤器,它的作用就是当用户请求一个url之前,可以预先做一些处理,当请求完url返回给用户之前还可以做一些处理(所以filter的功能十分的强大,如果想做的话可以强大到让用户,不能访问请求的url的程度。。。)<?XML:NAMESPACE PREFIX = O ?>
接口javax.servlet.Filter
void init(FilterConfig filterConfig)
可以在此方法中完成Filter需要初始化的内容,在一个filter提供服务之前肯定会先调用此方法
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
当用户访问的url或者servlet被配置了使用此filter时,servlet/jsp容器会调用此方法,每次访问调用一次。chain参数为一个FilterChain接口的对象,可以简单的理解成chain包含了访问一个url上所有的需要调用的方法。
void destroy()
当不需要一个filter再提供服务时调用,容器会调用此方法
filter的工作模式:
![](https://p-blog.csdn.net/images/p_blog_csdn_net/Squall1009/filter.gif)
可以看到假如用户访问的一个url有两个filter的时候。
servlet容器会先调用最外层的filter(Filter1)的doFilter()方法,然后在由Filter1中调用的chain.doFilter方法将控制权返回给容器,接着容器调用第二层filter(Filter2)的doFilter方法,然后在由Filter2中调用的chain.doFilter方法将控制权返回给容器,这个时候容器才能去访问真正的需要请求的servlet和jsp,但servlet和jsp执行完毕以后,会运行Filter2中doFilter方法调用的chain.doFilter方法后剩余的代码片断,之后再运行Filter1中doFilter方法调用的chain.doFilter方法后剩余的代码片断。
Filter对应的请求方式有4种:request、forward、include、error
request:当用户直接请求一个网页时才会通过此filter,当用户通过RequestDispatcher(会再后面详细介绍该类的方法)的forward不会通过该filter
forward: 当用户通过RequestDispatcher的forward方法访问对应的url时才会通过该filter
include:当用户通过RequestDispatcher的include方法访问对应的url时才会通过该filter
error:当用户的请求通过错误处理机制的时候才会通过该filter(目前不准备详细论述)。
一个filter可以对应几种请求方式,但是如果配置时不写请求方式的话,默认方式为request。
下面先举一个例子看下filter工作的过程:
FilterFirst:
package
squall.servlet.filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public
class
FilterFirst
implements
Filter
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void destroy()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest request, ServletResponse response,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FilterChain chain) throws IOException, ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("Begin First Filter!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
chain.doFilter(request, response);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("After First Filter!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void init(FilterConfig config) throws ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
FilterSecond:
package
squall.servlet.filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public
class
FilterSecond
implements
Filter
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void destroy()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest request, ServletResponse response,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FilterChain chain) throws IOException, ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("Begin Second Filter!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
chain.doFilter(request, response);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("After Second Filter!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void init(FilterConfig config) throws ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
部署时修改web.xml如下:
在第一个<servlet>标签前添加:
<
filter
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
FilterFirst
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-class
>
squall.servlet.filter.FilterFirst
</
filter-class
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
FilterSecond
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-class
>
squall.servlet.filter.FilterSecond
</
filter-class
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
FilterFirst
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
url-pattern
>
/HelloServlet
</
url-pattern
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
FilterSecond
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
url-pattern
>
/HelloServlet
</
url-pattern
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
将filter标签放置在一起,将filter-mapping标签放在再一起,顺序根据filter-name一致。这里的filter-mapping的顺序即为:假如我们访问的一个url有两个filter时,filter的先后顺序,web.xml中filter-mapping在前面的filter先被执行。
部署完毕之后我们访问:http://127.0.0.1:8080/train/HelloServlet
可以看到tomcat管理控制台输出如下:
Begin First Filter!
Begin Second Filter!
After Second Filter!
After First Filter!
上面的例子可以看出filter工作的过程和我们上面的图片一致。
下面我们来举一个比较贴近实际的例子看一下filter的方便之处。
下面的例子用于常见的解决中文乱码的问题,我们为了防止中文乱码经常需要调用
HttpServletRequest和HttpServletResponse的setCharacterEncoding方法,底下的这个filter的例子实现了,采用filter来解决这个问题。
Filterparam.html:
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
head
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
META
http-equiv
="Content-Type"
content
="text/html; charset=GBK"
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
<
script
type
="text/javascript"
>
...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function changeaction()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var a = document.getElementsByName("filter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(i = 0; i < a.length; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a[i].checked==true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fm.action=a[i].value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</
script
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
title
>
filterparameter.html
</
title
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
head
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
body
bgcolor
="#FDF5E6"
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
h2
>
Filter中文乱码示范
</
h2
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
form
name
="fm"
method
="post"
action
="FilterShowParameter/No"
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
p
>
姓名:
<
input
type
="text"
name
="username"
size
="15"
/>
</
p
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
p
>
使用filter:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
input
name
="filter"
value
="FilterShowParameter/No"
type
="radio"
onclick
="changeaction()"
checked
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
不使用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
input
name
="filter"
value
="FilterShowParameter/Yes"
type
="radio"
onclick
="changeaction()"
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
使用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
p
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
p
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
input
value
="确认"
type
="submit"
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
input
value
="清除"
type
="reset"
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
p
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
form
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
body
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
html
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
直接放在train目录下
FilterShow:
package
squall.servlet.basic;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.PrintWriter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.http.HttpServlet;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.http.HttpServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.http.HttpServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public
class
FilterShow
extends
HttpServlet
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
showParam( req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throws ServletException, IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
showParam( req, resp);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void showParam(HttpServletRequest req, HttpServletResponse resp) throws IOException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
resp.setContentType("text/html");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PrintWriter out = resp.getWriter();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String docType = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> ";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String name = req.getParameter("username");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println(docType + "<HTML> " + "<HEAD><TITLE> showparam"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ "</TITLE></HEAD> " + "<BODY BGCOLOR="#FDF5E6"> "
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+" 姓名:" + name + " </br>"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
out.println("</body></html>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
部署时将FilterShow mapping至/FilterShowParameter/*
EncodingFilter:
package
squall.servlet.filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public
class
EncodingFilter
implements
Filter
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void init(FilterConfig config) throws ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String str = config.getInitParameter("encoding");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (str != null && !"".equals(str))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
encoding = str;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest request, ServletResponse response,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FilterChain chain) throws IOException, ServletException
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("encoding is " + encoding);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(encoding != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
request.setCharacterEncoding(encoding);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
response.setCharacterEncoding(encoding);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
chain.doFilter(request, response);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void destroy()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String encoding = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
EncodingFilter的功能就是将request和response setCharacterEncoding为预先定义好的encoding。getInitParameter 将在以后介绍。
EncodingFilter部署方式:
在web.xml最后一个</filter>下添加
<
filter
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
EncodingFilter
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-class
>
squall.servlet.filter.EncodingFilter
</
filter-class
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
init-param
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
param-name
>
encoding
</
param-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
param-value
>
GBK
</
param-value
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
init-param
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter
>
然后在最后一个</filter-mapping>下添加:
<
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
EncodingFilter
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
url-pattern
>
/FilterShowParameter/Yes
</
url-pattern
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
dispatcher
>
REQUEST
</
dispatcher
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
dispatcher
>
FORWARD
</
dispatcher
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter-mapping
>
部署完毕后访问http://127.0.0.1:8080/train/filterparam.html
输入姓名为中文以后,如果使用filter 选择为不使用则会看到乱码
选择使用filter后则会看到正确的中文。
上面的例子是对REQUEST和FORWARD使用filter。
关于filter-mapping的include方式做一个简单的说明,如下:
<
filter-mapping
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
filter-name
>
FilterName
</
filter-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
servlet-name
>
HelloServlet
</
servlet-name
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<
dispatcher
>
INCLUDE
</
dispatcher
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</
filter-mapping
>
include方式时必须将<url-pattern>修改为<servlet-name>
上面的filter-mapping的含义为:
当HelloServlet这个servlet里调用RequestDispatcher的include时,才会通过此filter。
filter的功能远远不只上面举的例子,filter还可以对servlet和jsp输出至客户端的内容进行修改,还可以用来判断是否满足一些条件,假如不满足条件就不让访问者访问相应的条件,这里就不做一一的介绍了。