首页 > 代码库 > Servlet的尾(yi)巴---filter ( 过滤器 )的小应用

Servlet的尾(yi)巴---filter ( 过滤器 )的小应用

        该,该,该.......,继之前说到的 Filter 现在用这个来做一个小小的应用----------》  铛,铛,铛,铛.....  ->_->      <丑逼的留言板>  

      要求:

           

  ----------没错,就是这样丑逼:

                           

 

     然后就是一个代码:(JSP部分)

                

 

 

    web.xml 文件部分

        

      Servlet  类:

    

    Filter 类:

   

 1 package Demo; 2  3 import java.io.IOException; 4  5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 import javax.servlet.http.HttpServletRequest;12 import javax.servlet.http.HttpServletResponse;13 14 public class Liuyanban implements Filter {15 16     @Override17     public void destroy() {18         // TODO Auto-generated method stub19 20     }21 22     @Override23     public void doFilter(ServletRequest request, ServletResponse response,24             FilterChain chain) throws IOException, ServletException {25         // TODO Auto-generated method stub26         27         HttpServletRequest req = (HttpServletRequest)request ;28         HttpServletResponse resp =(HttpServletResponse) response ;29         String username = req.getParameter("username");30         String comment = req.getParameter("comment");31         username=username.replace("zhang","gong");32         comment=comment.replace("animal","****");33         req.setAttribute("username",username);34         req.setAttribute("comment",comment);35         chain.doFilter(request,response);36 37     }38 39     @Override40     public void init(FilterConfig filterConfig) throws ServletException {41         // TODO Auto-generated method stub42 43     }44 45 }
View Code

   然后显示结果:(  Result.jsp     )

   

   最终的效果为:

       

 

Servlet的尾(yi)巴---filter ( 过滤器 )的小应用