Servlet中的过滤器Filter用法

时间:2021-06-29 17:01:35

1.过滤器的概念

Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应。 主要用于对HttpServletRequest 进行预处理,也可以对HttpServletResponse 进行后处理,是个典型的处理链。

优点:过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别注意过滤链的执行顺序问题

2.过滤器的作用描述

  • 在HttpServletRequest 到达Servlet 之前,拦截客户的HttpServletRequest 。
  • 根据需要检查HttpServletRequest ,也可以修改HttpServletRequest 头和数据。
  • 在HttpServletResponse 到达客户端之前,拦截HttpServletResponse 。
  • 根据需要检查HttpServletResponse ,可以修改HttpServletResponse 头和数据。

3.过滤器的执行流程

Servlet中的过滤器Filter用法Servlet中的过滤器Filter用法

4.Filter接口

1.如何驱动

在 web 应用程序启动时,web 服务器将根据 web.xml 文件中的配置信息来创建每个注册的 Filter 实例对象,并将其保存在服务器的内存中

2.方法介绍

    • init()  Init 方法在 Filter 生命周期中仅执行一次,web 容器在调用 init 方法时
    • destory()  在Web容器卸载 Filter 对象之前被调用。该方法在Filter的生命周期中仅执行一次。在这个方法中,可以释放过滤器使用的资源。
    • doFilter() Filter 链的执行

5.FilterChain接口

1.如何实例化

代表当前 Filter 链的对象。由容器实现,容器将其实例作为参数传入过滤器对象的doFilter()方法中

2.作用

调用过滤器链中的下一个过滤器

filter实例:

web.xml配置

  1. <!-- 编码过滤器 -->
  2. <filter>
  3. <filter-name>setCharacterEncoding</filter-name>
  4. <filter-class>com.company.strutstudy.web.servletstudy.filter.EncodingFilter</filter-class>
  5. <init-param>
  6. <param-name>encoding</param-name>
  7. <param-value>utf-8</param-value>
  8. </init-param>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>setCharacterEncoding</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>
  14. <!-- 请求url日志记录过滤器 -->
  15. <filter>
  16. <filter-name>logfilter</filter-name>
  17. <filter-class>com.company.strutstudy.web.servletstudy.filter.LogFilter</filter-class>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>logfilter</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>

编码拦截器:

  1. public class EncodingFilter implements Filter {
  2. private String encoding;
  3. private Map<String, String> params = new HashMap<String, String>();
  4. // 项目结束时就已经进行销毁
  5. public void destroy() {
  6. System.out.println("end do the encoding filter!");
  7. params=null;
  8. encoding=null;
  9. }
  10. public void doFilter(ServletRequest req, ServletResponse resp,
  11. FilterChain chain) throws IOException, ServletException {
  12. //UtilTimerStack.push("EncodingFilter_doFilter:");
  13. System.out.println("before encoding " + encoding + " filter!");
  14. req.setCharacterEncoding(encoding);
  15. // resp.setCharacterEncoding(encoding);
  16. // resp.setContentType("text/html;charset="+encoding);
  17. chain.doFilter(req, resp);
  18. System.out.println("after encoding " + encoding + " filter!");
  19. System.err.println("----------------------------------------");
  20. //UtilTimerStack.pop("EncodingFilter_doFilter:");
  21. }
  22. // 项目启动时就已经进行读取
  23. public void init(FilterConfig config) throws ServletException {
  24. System.out.println("begin do the encoding filter!");
  25. encoding = config.getInitParameter("encoding");
  26. for (Enumeration e = config.getInitParameterNames(); e
  27. .hasMoreElements();) {
  28. String name = (String) e.nextElement();
  29. String value = config.getInitParameter(name);
  30. params.put(name, value);
  31. }
  32. }
  33. }

日志拦截器:

  1. public class LogFilter implements Filter {
  2. FilterConfig config;
  3. public void destroy() {
  4. this.config = null;
  5. }
  6. public void doFilter(ServletRequest req, ServletResponse res,
  7. FilterChain chain) throws IOException, ServletException {
  8. // 获取ServletContext 对象,用于记录日志
  9. ServletContext context = this.config.getServletContext();
  10. //long before = System.currentTimeMillis();
  11. System.out.println("before the log filter!");
  12. //context.log("开始过滤");
  13. // 将请求转换成HttpServletRequest 请求
  14. HttpServletRequest hreq = (HttpServletRequest) req;
  15. // 记录日志
  16. System.out.println("Log Filter已经截获到用户的请求的地址:"+hreq.getServletPath() );
  17. //context.log("Filter已经截获到用户的请求的地址: " + hreq.getServletPath());
  18. try {
  19. // Filter 只是链式处理,请求依然转发到目的地址。
  20. chain.doFilter(req, res);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. System.out.println("after the log filter!");
  25. //long after = System.currentTimeMillis();
  26. // 记录日志
  27. //context.log("过滤结束");
  28. // 再次记录日志
  29. //context.log(" 请求被定位到" + ((HttpServletRequest) req).getRequestURI()
  30. //      + "所花的时间为: " + (after - before));
  31. }
  32. public void init(FilterConfig config) throws ServletException {
  33. System.out.println("begin do the log filter!");
  34. this.config = config;
  35. }
  36. }

HelloServlet类:

  1. public class HelloWorldServlet extends HttpServlet{
  2. /**
  3. * 查看httpservlet实现的service一看便知,起到了一个controll控制器的作用(转向的)
  4. * 之后便是跳转至我们熟悉的doget,dopost等方法中
  5. */
  6. @Override
  7. protected void service(HttpServletRequest req, HttpServletResponse resp)
  8. throws ServletException, IOException {
  9. System.out.println("doservice..."+this.getInitParameter("encoding"));
  10. super.service(req, resp);
  11. }
  12. @Override
  13. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  14. throws ServletException, IOException {
  15. System.out.println("doget...");
  16. doPost(req, resp);
  17. }
  18. @Override
  19. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  20. throws ServletException, IOException {
  21. System.out.println("dopost...");
  22. }
  23. }

结果:

  1. before encoding utf-8 filter!
  2. before the log filter!
  3. Log Filter已经截获到用户的请求的地址:/hello
  4. doservice...UTF-8
  5. doget...
  6. dopost...
  7. after the log filter!
  8. after encoding utf-8 filter!
  9. ----------------------------------------

总结:

1.过滤器执行流程

2.常用过滤器

转载自http://blog.csdn.net/techbirds_bao/article/details/8243800

<div id="article_content" class="article_content">

<p>1.过滤器的概念</p>
<p style="text-align:left; text-indent:28px"><span style="font-family:Arial,sans-serif"><span style="line-height:28px"><span style="font-size:10px"><a href="http://lib.csdn.net/base/javaee" class="replace_word" title="Java EE知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java</a>中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应。 主要用于对HttpServletRequest 进行预处理,也可以对HttpServletResponse 进行后处理,是个典型的处理链。</span><br>
</span></span></p>
<p style="text-align:left; text-indent:28px"><span style="font-family:Arial,sans-serif; font-size:10px"><span style="line-height:28px">优点:过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别注意过滤链的执行顺序问题</span></span></p>
<p>2.过滤器的作用描述</p>
<p></p>
<ul>
<li><span style="white-space:pre">在HttpServletRequest 到达Servlet 之前,拦截客户的HttpServletRequest 。&nbsp;</span></li><li><span style="white-space:pre">&nbsp; </span><span style="white-space:pre">根据需要检查HttpServletRequest ,也可以修改HttpServletRequest 头和数据。&nbsp;</span></li><li><span style="white-space:pre">&nbsp;</span><span style="white-space:pre"> </span><span style="white-space:pre">在HttpServletResponse 到达客户端之前,拦截HttpServletResponse 。&nbsp;</span></li><li><span style="white-space:pre">根据需要检查HttpServletResponse ,可以修改HttpServletResponse 头和数据。</span></li></ul>
<p></p>
<p>3.过滤器的执行流程</p>
<p style="text-align:right"><span style="white-space:pre"><img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHAvaW1nLm15LmNzZG4ubmV0L3VwbG9hZHMvMjAxMjExLzMwLzEzNTQyNjE2NzJfODQ1MC5qcGc=.jpg" alt=""><img src="//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHAvaW1nLm15LmNzZG4ubmV0L3VwbG9hZHMvMjAxMjExLzMwLzEzNTQyNjEzNDNfOTMwMC5wbmc=.jpg" alt="" style="white-space:pre; text-align:left"></span></p>
<p style="text-align:left"><span style="white-space:pre"><br>
</span></p>
<p>4.Filter接口</p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p>1.如何驱动</p>
</blockquote>
<p><span style="white-space:pre"></span></p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p>在 web 应用程序启动时,web 服务器将根据 web.xml 文件中的配置信息来创建每个注册的 Filter 实例对象,并将其保存在服务器的内存中</p>
</blockquote>
</blockquote>
<p><span style="white-space:pre"></span></p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p>2.方法介绍</p>
</blockquote>
<p><span style="white-space:pre"></span></p>
<p></p>
<ul>
<ul>
<li>init() &nbsp;Init 方法在 Filter 生命周期中仅执行一次,web 容器在调用 init 方法时</li><li>destory() &nbsp;在Web容器卸载 Filter 对象之前被调用。该方法在Filter的生命周期中仅执行一次。在这个方法中,可以释放过滤器使用的资源。</li><li>doFilter() Filter 链的执行&nbsp;</li></ul>
</ul>
<p></p>
<p>5.FilterChain接口</p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p>1.如何实例化</p>
</blockquote>
<p><span style="white-space:pre"></span></p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p><span style="white-space:pre">代表当前 Filter 链的对象。由容器实现,容器将其实例作为参数传入过滤器对象的doFilter()方法中</span></p>
</blockquote>
</blockquote>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p>2.作用</p>
</blockquote>
<p><span style="white-space:pre"></span></p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p><span style="white-space:pre">调用过滤器链中的下一个过滤器</span></p>
</blockquote>
</blockquote>
<p>filter实例:</p>
<p>web.xml配置</p>
<p><span style="font-family:monospace"><span style="white-space:pre"></span></span></p>
<div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 463px; top: 1519px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_1" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span class="comments">&lt;!--&nbsp;编码过滤器&nbsp;--&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>setCharacterEncoding</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>com.company.strutstudy.web.servletstudy.filter.EncodingFilter</span><span class="tag">&lt;/</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">init-param</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">param-name</span><span class="tag">&gt;</span><span>encoding</span><span class="tag">&lt;/</span><span class="tag-name">param-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">param-value</span><span class="tag">&gt;</span><span>utf-8</span><span class="tag">&lt;/</span><span class="tag-name">param-value</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">init-param</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>setCharacterEncoding</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>/*</span><span class="tag">&lt;/</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span><span class="comments">&lt;!--&nbsp;请求url日志记录过滤器&nbsp;--&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>logfilter</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>com.company.strutstudy.web.servletstudy.filter.LogFilter</span><span class="tag">&lt;/</span><span class="tag-name">filter-class</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>logfilter</span><span class="tag">&lt;/</span><span class="tag-name">filter-name</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>/*</span><span class="tag">&lt;/</span><span class="tag-name">url-pattern</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="tag">&lt;/</span><span class="tag-name">filter-mapping</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="html" style="display: none;">&lt;!-- 编码过滤器 --&gt;
     &lt;filter&gt;
         &lt;filter-name&gt;setCharacterEncoding&lt;/filter-name&gt;
         &lt;filter-class&gt;com.company.strutstudy.web.servletstudy.filter.EncodingFilter&lt;/filter-class&gt;
         &lt;init-param&gt;
             &lt;param-name&gt;encoding&lt;/param-name&gt;
             &lt;param-value&gt;utf-8&lt;/param-value&gt;
         &lt;/init-param&gt;
     &lt;/filter&gt;
     &lt;filter-mapping&gt;
         &lt;filter-name&gt;setCharacterEncoding&lt;/filter-name&gt;
         &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
     &lt;/filter-mapping&gt;
 
&lt;!-- 请求url日志记录过滤器 --&gt;
     &lt;filter&gt;
         &lt;filter-name&gt;logfilter&lt;/filter-name&gt;
         &lt;filter-class&gt;com.company.strutstudy.web.servletstudy.filter.LogFilter&lt;/filter-class&gt;
     &lt;/filter&gt;
     &lt;filter-mapping&gt;
         &lt;filter-name&gt;logfilter&lt;/filter-name&gt;
         &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
     &lt;/filter-mapping&gt;</pre><span style="white-space:pre"></span><br>
编码拦截器:
<p></p>
<p><span style="font-family:monospace"><span style="white-space:pre"></span></span></p>
<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 462px; top: 2049px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_2" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;EncodingFilter&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Filter&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;String&nbsp;encoding;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;Map&lt;String,&nbsp;String&gt;&nbsp;params&nbsp;=&nbsp;</span><span class="keyword">new</span><span>&nbsp;HashMap&lt;String,&nbsp;String&gt;();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;项目结束时就已经进行销毁</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;destroy()&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"end&nbsp;do&nbsp;the&nbsp;encoding&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params=<span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encoding=<span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;doFilter(ServletRequest&nbsp;req,&nbsp;ServletResponse&nbsp;resp,&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FilterChain&nbsp;chain)&nbsp;<span class="keyword">throws</span><span>&nbsp;IOException,&nbsp;ServletException&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//UtilTimerStack.push("EncodingFilter_doFilter:");</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"before&nbsp;encoding&nbsp;"</span><span>&nbsp;+&nbsp;encoding&nbsp;+&nbsp;</span><span class="string">"&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;req.setCharacterEncoding(encoding);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;resp.setCharacterEncoding(encoding);</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;resp.setContentType("text/html;charset="+encoding);</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chain.doFilter(req,&nbsp;resp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"after&nbsp;encoding&nbsp;"</span><span>&nbsp;+&nbsp;encoding&nbsp;+&nbsp;</span><span class="string">"&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(<span class="string">"----------------------------------------"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//UtilTimerStack.pop("EncodingFilter_doFilter:");</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;项目启动时就已经进行读取</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;init(FilterConfig&nbsp;config)&nbsp;</span><span class="keyword">throws</span><span>&nbsp;ServletException&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"begin&nbsp;do&nbsp;the&nbsp;encoding&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;encoding&nbsp;=&nbsp;config.getInitParameter(<span class="string">"encoding"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">for</span><span>&nbsp;(Enumeration&nbsp;e&nbsp;=&nbsp;config.getInitParameterNames();&nbsp;e&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.hasMoreElements();)&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;name&nbsp;=&nbsp;(String)&nbsp;e.nextElement();&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;value&nbsp;=&nbsp;config.getInitParameter(name);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params.put(name,&nbsp;value);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;}&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="java" style="display: none;">public class EncodingFilter implements Filter {
     private String encoding;
     private Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
     // 项目结束时就已经进行销毁
     public void destroy() {
         System.out.println("end do the encoding filter!");
         params=null;
         encoding=null;
     }
     public void doFilter(ServletRequest req, ServletResponse resp,
             FilterChain chain) throws IOException, ServletException {
         //UtilTimerStack.push("EncodingFilter_doFilter:");
         System.out.println("before encoding " + encoding + " filter!");
         req.setCharacterEncoding(encoding);
         // resp.setCharacterEncoding(encoding);
         // resp.setContentType("text/html;charset="+encoding);
         chain.doFilter(req, resp);        
         System.out.println("after encoding " + encoding + " filter!");
         System.err.println("----------------------------------------");
         //UtilTimerStack.pop("EncodingFilter_doFilter:");
     }
 
     // 项目启动时就已经进行读取
     public void init(FilterConfig config) throws ServletException {
         System.out.println("begin do the encoding filter!");
         encoding = config.getInitParameter("encoding");
         for (Enumeration e = config.getInitParameterNames(); e
                 .hasMoreElements();) {
             String name = (String) e.nextElement();
             String value = config.getInitParameter(name);
             params.put(name, value);
         }
     }
 }</pre>日志拦截器:<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 462px; top: 2751px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_3" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;LogFilter&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Filter&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;FilterConfig&nbsp;config;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;destroy()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">this</span><span>.config&nbsp;=&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;doFilter(ServletRequest&nbsp;req,&nbsp;ServletResponse&nbsp;res,&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FilterChain&nbsp;chain)&nbsp;<span class="keyword">throws</span><span>&nbsp;IOException,&nbsp;ServletException&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;获取ServletContext&nbsp;对象,用于记录日志</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ServletContext&nbsp;context&nbsp;=&nbsp;<span class="keyword">this</span><span>.config.getServletContext();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//long&nbsp;before&nbsp;=&nbsp;System.currentTimeMillis();</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"before&nbsp;the&nbsp;log&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//context.log("开始过滤");</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;将请求转换成HttpServletRequest&nbsp;请求</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest&nbsp;hreq&nbsp;=&nbsp;(HttpServletRequest)&nbsp;req;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;记录日志</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"Log&nbsp;Filter已经截获到用户的请求的地址:"</span><span>+hreq.getServletPath()&nbsp;);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//context.log("Filter已经截获到用户的请求的地址:&nbsp;"&nbsp;+&nbsp;hreq.getServletPath());</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">try</span><span>&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;Filter&nbsp;只是链式处理,请求依然转发到目的地址。</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chain.doFilter(req,&nbsp;res);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">catch</span><span>&nbsp;(Exception&nbsp;e)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"after&nbsp;the&nbsp;log&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//long&nbsp;after&nbsp;=&nbsp;System.currentTimeMillis();</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;记录日志</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//context.log("过滤结束");</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;再次记录日志</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//context.log("&nbsp;请求被定位到"&nbsp;+&nbsp;((HttpServletRequest)&nbsp;req).getRequestURI()</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;"所花的时间为:&nbsp;"&nbsp;+&nbsp;(after&nbsp;-&nbsp;before));</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;init(FilterConfig&nbsp;config)&nbsp;</span><span class="keyword">throws</span><span>&nbsp;ServletException&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"begin&nbsp;do&nbsp;the&nbsp;log&nbsp;filter!"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">this</span><span>.config&nbsp;=&nbsp;config;&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;}&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="java" style="display: none;">public class LogFilter implements Filter {
     FilterConfig config;
 
     public void destroy() {
         this.config = null;
     }
 
     public void doFilter(ServletRequest req, ServletResponse res,
             FilterChain chain) throws IOException, ServletException {
         // 获取ServletContext 对象,用于记录日志
         ServletContext context = this.config.getServletContext();
         //long before = System.currentTimeMillis();
         System.out.println("before the log filter!");
         //context.log("开始过滤");
         // 将请求转换成HttpServletRequest 请求
         HttpServletRequest hreq = (HttpServletRequest) req;
         // 记录日志
         System.out.println("Log Filter已经截获到用户的请求的地址:"+hreq.getServletPath() );
         //context.log("Filter已经截获到用户的请求的地址: " + hreq.getServletPath());
         try {
             // Filter 只是链式处理,请求依然转发到目的地址。
             chain.doFilter(req, res);
         } catch (Exception e) {
             e.printStackTrace();
         }
         System.out.println("after the log filter!");
         //long after = System.currentTimeMillis();
         // 记录日志
         //context.log("过滤结束");
         // 再次记录日志
         //context.log(" 请求被定位到" + ((HttpServletRequest) req).getRequestURI()
         //        + "所花的时间为: " + (after - before));
     }
 
     public void init(FilterConfig config) throws ServletException {
         System.out.println("begin do the log filter!");
         this.config = config;
     }
 
 }</pre>HelloServlet类:<div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 462px; top: 3561px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_4" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;HelloWorldServlet&nbsp;</span><span class="keyword">extends</span><span>&nbsp;HttpServlet{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/**</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;查看httpservlet实现的service一看便知,起到了一个controll控制器的作用(转向的)</span>&nbsp;</span></li><li class="alt"><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;之后便是跳转至我们熟悉的doget,dopost等方法中&nbsp;</span>&nbsp;</span></li><li class=""><span><span class="comment">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;service(HttpServletRequest&nbsp;req,&nbsp;HttpServletResponse&nbsp;resp)&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throws</span><span>&nbsp;ServletException,&nbsp;IOException&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"doservice..."</span><span>+</span><span class="keyword">this</span><span>.getInitParameter(</span><span class="string">"encoding"</span><span>));&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">super</span><span>.service(req,&nbsp;resp);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;doGet(HttpServletRequest&nbsp;req,&nbsp;HttpServletResponse&nbsp;resp)&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throws</span><span>&nbsp;ServletException,&nbsp;IOException&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"doget..."</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;doPost(req,&nbsp;resp);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;doPost(HttpServletRequest&nbsp;req,&nbsp;HttpServletResponse&nbsp;resp)&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throws</span><span>&nbsp;ServletException,&nbsp;IOException&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"dopost..."</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;}&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="java" style="display: none;">public class HelloWorldServlet extends HttpServlet{
 
     /**
      * 查看httpservlet实现的service一看便知,起到了一个controll控制器的作用(转向的)
      * 之后便是跳转至我们熟悉的doget,dopost等方法中    
      */
     @Override
     protected void service(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
         System.out.println("doservice..."+this.getInitParameter("encoding"));
         
         super.service(req, resp);
     }
 
     @Override
     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
         System.out.println("doget...");
         doPost(req, resp);
     }
 
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
         System.out.println("dopost...");
     }
     
     
 
 }</pre>结果:<br>
<p><span style="font-family:monospace"><span style="white-space:pre"></span></span></p><div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 464px; top: 4191px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_5" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1"><li class="alt"><span><span>before&nbsp;encoding&nbsp;utf-8&nbsp;filter!&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;before&nbsp;the&nbsp;log&nbsp;filter!&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;Log&nbsp;Filter已经截获到用户的请求的地址:/hello&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;doservice...UTF-8&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;doget...&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;dopost...&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;after&nbsp;the&nbsp;log&nbsp;filter!&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;after&nbsp;encoding&nbsp;utf-8&nbsp;filter!&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;----------------------------------------&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="plain" style="display: none;">before encoding utf-8 filter!
  before the log filter!
  Log Filter已经截获到用户的请求的地址:/hello
  doservice...UTF-8
  doget...
  dopost...
  after the log filter!
  after encoding utf-8 filter!
  ----------------------------------------</pre><p></p>
<p><span style="font-family:monospace"><span style="white-space:pre">总结:</span></span></p>
<blockquote style="margin:0 0 0 40px; border:none; padding:0px">
<p><span style="font-family:monospace"><span style="white-space:pre">1.过滤器执行流程</span></span></p>
<p><span style="font-family:monospace"><span style="white-space:pre">2.常用过滤器</span></span></p>
</blockquote>
<div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 463px; top: 4495px; width: 24px; height: 13px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_6" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&amp;width=24&amp;height=13" wmode="transparent" width="24" align="middle" height="13"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"code"</span><span>&nbsp;</span><span class="attribute">class</span><span>=</span><span class="attribute-value">"plain"</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">pre</span><span>&nbsp;</span><span class="attribute">name</span><span>=</span><span class="attribute-value">"code"</span><span>&nbsp;</span><span class="attribute">class</span><span>=</span><span class="attribute-value">"plain"</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="tag">&lt;</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="alt"><span><span class="tag">&lt;/</span><span class="tag-name">pre</span><span class="tag">&gt;</span><span>&nbsp;&nbsp;</span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249" style="display: none;"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="html" style="display: none;"><div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_7" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&amp;width=0&amp;height=0" wmode="transparent" width="0" align="middle" height="0"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1"><li class="alt"><span><span>&lt;pre&gt;&lt;/pre&gt;&lt;pre&nbsp;name="code"&nbsp;class="plain"&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></span></li><li class=""><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class=""><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class=""><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class="alt"><span>&lt;pre&gt;&lt;/pre&gt;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="plain" style="display: none;"><pre></pre><div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_8" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&amp;width=0&amp;height=0" wmode="transparent" width="0" align="middle" height="0"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a></div></div><ol start="1"><li class="alt"><span><span>&nbsp;&nbsp;</span></span></li></ol><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets.png"></a></div></div><pre name="code" class="plain" style="display: none;"></pre><pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
<pre></pre>
   
</pre></pre></div>