过滤器的简单实例

过滤器的简单实例

思路: 在web.xml配置过滤器(过滤器的作用是过滤一些用户访问的页面是否合法,然后进行处理)

       web.xml

<filter>

<filter-name>qnFilter</filter-name>

<filter-class>com.bdqn.core.QnFilter</filter-class> 

</filter>

<filter-mapping>

<filter-name>qnFilter</filter-name>

<url-pattern>/*</url-pattern>

</flter-mapping>

 

 com.bdqn.core.QnFilter.java

public void doFilter(ServletRequest arg0, ServletResponse arg1,
   FilterChain chain) throws IOException, ServletException {

    //将ServletRequest转换为HttpServletRequest

    HttpServletRequest  req=(HttpServletRequest)arg0;

    //得到请求的url地址

     String uri=req.getRequestURI();

   //打印

    System.out.println(uri);

    //得到请求的主要地址

    String contextPath=req.getContextPath();

    //把uri替换成空格

    String path=uri.replaceFilrst(contextPath+"/","");

    //打印 http://localhost:8080/strTest/test 结果test

       System.out.printlnI(path);

    //进行判断

      if("test”.equals(path)){

   //如果地址是http://localhost:8080/strTest/test

 

       req.getRequestDispathcher("test.jsp").forward(arg0,arg1);

       return ;

        else  if("path".equals(path)){

         req.getRequestDispathcher("path.jsp").forward(arg0,arg1);

             return ;

}

      chain.doFilter(arg0,arg1);

}

转载于:https://www.cnblogs.com/5scxy/articles/5804264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值