Filter过滤器

Filter过滤器

概述:

Servlet API中提供了一个Filter接口,开发web应用时,如果编写的Java类实现了这个接口,则把这个java类称之为过滤器Filter。通常也被称作拦截器。

Filter的创建和销毁有web服务器完成。

Filter如何拦截:

WEB服务器每次在调用servlet的service方法之前,会在xml文件中查找当前url地址是否被拦截,如果有,会调用对应的filter对象的doFilter方法。

常用的过滤器

1、检查用户是否登录的过滤器

(1)、登录页面(login.jsp)

<body>

<center>

    <form action="dologin.jsp" method="post">

      <input type="text" name="username"/>

      <input type="submit" value="LOGIN"/>

   </form>

</center>

</body>


(2)、登录成功页面(ok.jsp)

<h3>登录成功</h3>


(3)、跳转控制页面(dologin.jsp)

<%

   String username=request.getParameter("username");   //获取登录信息

   if(username!=null && !username.trim().equals("")){   //若登录信息正确,则写入session

      session.setAttribute(application.getInitParameter("usersessionkey"),username);

      response.sendRedirect("ok.jsp");   //重定向到登录成功页面

   }else{

      response.sendRedirect("login.jsp");   //重定向到登录页面

   }

%>


(4)、过滤器(LoginFilter.java)

public class LoginFilter implements Filter{

   private String usersessionkey;   //用户信息

   private String redirectPage;   //重定向页面

   private String uncheckURL;   //不检查的url

   public LoginFilter(){}   //构造方法

   public void destroy(){}   //销毁方法

   public void foFilter(ServletRequest request,Servlet Response,filterChain chain) throws IOException, ServletException{

       //获取请求路径

      HttpServletRequest req=(HttpServletRequest)request;

      HttpServletResponse resp=(HttpServletResponse)response;

      String ServletPath=req.getServletPath();

      //检查请求路径是否是不需过滤的,若是则直接放行,程序结束

      List<String> list=Arrays.asList(uncheckURL.split(","));

      if(list.contains(ServletPath)){

         chain.doFilter(request,response);

         return;

      }

      //从session中获取usersessionkey的值,若为null,则重定向到redirectPage

      Object user=req.getSession().getAttribute(usersessionkey);

      if(user==null){

         resp.sendRedirect(req.getContextPath()+redirectPage);

        return;

      }

      chain.doFilter(request,response);

   }

   public void init(FilterConfig fConfig) throws ServletException{

      ServletContext context=fConfig.getServletContext();

      usersessionkey=context.getInitParameter("usersessionkey");

      redirectPage=context.getInitParameter("redirectPage");

      uncheckURL=context.getInitParameter("uncheckURL");

   }

}


(5)、配置web.xml

<context-param>

   <param-name>usersessionkey</param-name>

   <param-value>USERSESSIONKEY</param-value>

</context-param>

<context-param>

   <param-name>redirectPage</param-name>

   <param-value>/login.jsp</param-value>

</context-param>

<context-param>

   <param-name>uncheckURL</param-name>

   <param-value>/dologin.jsp,/login.jsp</param-value>

</context-param>

<filter>

   <filter-name>LoginFilter</filter-name>

   <filter-class>com.filter.LoginFilter</filter-class>

</filter>

<filter-mapping>

   <filter-name>LoginFilter</filter-name>

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

</filter-mapping>

上述代码:若没有输入直接登录,页面将会跳转到登录页面,让用户进行登录,若已经登录,则跳转到登录成功页面。


2、字符编码的过滤器

统一全站字符编码

request.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

(1)、过滤器类

public class EncodingFilter implements Filter{

private String encoding;

......

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {

   if(request.setCharacterEncoding==null){

      String encod=this.encoding;

      if(encod!=null){

          request.setCharacterEncoding(encod);

      }

     response.setContenType("text/html;charset=utf-8");

    chain.doFilter(request,response);

   }

}

public void init(FilterConfig fConfig) throws ServletException {

    this.encoding=fConfig.getInitParameter("encoding");

}

}


(2)、web.xml文件中

<filter>

   <filter-name></filter-name>

   <filter-class></filter-class>

   <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8<param-vale>

  </init-param>

</filter>

<filter-mapping>

   <filter-name></filter-name>

   <url-pattern></url-pattern>

</filter-mapping>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值