1.过滤器的概念

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

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

2.过滤器的作用描述

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

3.过滤器的执行流程

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="http://img.my.csdn.net/uploads/201211/30/1354261672_8450.jpg" alt=""><img src="http://img.my.csdn.net/uploads/201211/30/1354261343_9300.png" 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>

Servlet中的过滤器Filter用法的更多相关文章

  1. java Servlet中的过滤器Filter

    web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  2. Servlet中的过滤器Filter详解

    加载执行顺序 context-param->listener->filter->servlet web.xml中元素执行的顺序listener->filter->stru ...

  3. Servlet中的过滤器Filter

    链web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...

  4. Angularjs在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间实例

    Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期.格式化数字精度.语言本地化.格式化货币等等.但这些过滤器一般都是在VIEW中使用的,比 ...

  5. servlet中的过滤器 国际化

    1. 过滤器 基本概念 过滤器是需要在xml中配置的. 为什么需用到过滤器? 项目开发中,经常会涉及到重复代码的实现! 注册 ----à Servlet [1. 设置编码] ----à  JSP 修改 ...

  6. serlvet中的过滤器filter

    1.概念 过滤作用,对从客户端向服务器端发送的请求进行过滤,也可以对服务器端返回的响应进行处理.它使用户可以改变一个request和修改一个 response..Filter 不是一个servlet, ...

  7. java中的过滤器 --Filter

    package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.Filter ...

  8. Java过滤器—Filter用法简介

    一.什么是Filter? Filter译为过滤器. 由于年,Sun公司在Servlet2.3规范中添加了Filter功能,并在Servlet2.4中对Filter进行了细节上的补充. 二.运行原理: ...

  9. Servlet中的过滤器和监听器

    1.什么是过滤器? Servlet规范中定义的一种特殊的组件,用来拦截容器的调用过程.容器收到请求之后,首先调用过滤器,然后再调用Servlet 2.生命周期: 1.servlet:servlet的生 ...

随机推荐

  1. 好的 小图标 html

    只需引用样式,加入字体文件到项目中就可实现 <link href="css/font-awesome.min.css" rel="stylesheet"& ...

  2. HDU ACM 1495 非常可乐(广搜BFS)

    非常可乐 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  3. FZU 1914 Funny Positive Sequence(线性算法)

    这个当时我没有做出来,看了很多人包括学长的代码才懂,我感觉最好的方法还是下面那一种,标记以谁开头的是不行的,我感觉有点不好理解,如果不懂举组样例在纸上写一下就会比较清楚了 #include<io ...

  4. Ubuntu下安装Node.js

    下载Linux Binaries (.tar.gz)二进制包 解压 重命名为node 移动到/usr/local/目录下 创建软连接 ln -s /usr/local/node/bin/node   ...

  5. 转:Linux基本命令大全

    Linux基本命令大全   新手刚刚接触Linux的时候可能处处感到不便,不过没有关系,接触新的事物都有这样的一个过程,在你用过Linux一段时间后,你就会逐渐了解Linux其实和Windows一样容 ...

  6. Chapter 1 First Sight——16

    I drove around the school, following the line of traffic. 我开车绕学校随着交通线. 我开车穿过校园,紧跟着大部队. I was glad to ...

  7. Java项目开发第二天

    相对来说今天还是轻松了挺多的,好了,刚刚跑完步,今天心情不错,直接进入主题,就不废话了. (PS:室友都去准备迎新了,老大也是,还好都将课本留给我了,可以好好的看看书了,好好学习,天天向上.) 图形用 ...

  8. ptrace

    http://zhangwenxin82.blog.163.com/blog/static/114595956201171510512459/

  9. .htaccess 保护文件夹

    想要保护admin文件夹,经过以下两个步骤: 步骤一.可以用记事本新建文件.htaccess,输入以下内容: AuthType BasicAuth UserFile D:/AppServ/www/Hi ...

  10. js学习之函数

    1/.js中函数就是对象. 2/以表达式方式定义的函数一般不要函数名以使代码紧凑. 3/js中函数声明的方式会被默认提到外部脚本或最前面,所以在其定义前的代码也可调用. 然而以表达式方式的则不行. 4 ...