24H免费课程咨询  TEL:13401595960   QQ:1870218756  微信:13401595960(李老师)

东方博宜

网站首页 > 软件开发资讯 > Java开发

Java过滤器实例分析

2017-08-07 21:03:18 东方博宜 阅读

过滤器

过滤某些访问请求的类!

面向切面编程思想(AOP)的体现!

图片.png



例子:基本过滤器的配置,过滤jsp的请求!

LoginFilter

/**

 * 判断用户是否登陆的过滤器

 */

public class LoginFilter implements Filter{

 

         public void destroy() {

                   System.out.println("LoginFilter被销毁!");

         }

 

         public void doFilter(ServletRequest req, ServletResponse res,

                            FilterChain chain) throws IOException, ServletException {

                   System.out.println("LoginFilter调用成功!");

                  

                   //filter放行

                   chain.doFilter(req, res);

         }

 

         public void init(FilterConfig arg0) throws ServletException {

                   System.out.println("LoginFilter初始化!");

         }

 

}

 

web.xml

  <!-- 配置过滤器 -->

  <!-- 声明节点 -->

  <filter>

    <filter-name>LoginF</filter-name>

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

  </filter>

 

  <!-- 映射节点 -->

  <filter-mapping>

     <filter-name>LoginF</filter-name>

     <url-pattern>*.jsp</url-pattern>

  </filter-mapping>

 

例子:在LoginFilter中判断用户要访问的页面是否是登陆后才能访问的页面,如果是判断用户是否登陆,如果登陆了则放行,没有登陆则去登陆页面!

index.jsp

                <h1 align="center">

                            欢迎登陆

                   </h1>

   

        <!-- action表示提交的目的地址,method表示提交方式 post表示隐含提交  get表示以url带参数的形式提交(默认值) -->

                   <form action="LoginServlet" method="post">

                            <p>

                                     用户名:

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

                            </p>

 

                            <p>

                                     密码:

                                     <input type="password" name="password" />

                            </p>

 

                            <p>

                                     <input type="submit" value="提交" />

                                     <input type="reset" value="重置" />

                            </p>

                   </form>

 

LoginServlet

public class LoginServlet extends HttpServlet {

 

         public void doGet(HttpServletRequest request, HttpServletResponse response)

                            throws ServletException, IOException {

 

                   this.doPost(request, response);

         }

 

        

         public void doPost(HttpServletRequest request, HttpServletResponse response)

                            throws ServletException, IOException {

 

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

                   request.setCharacterEncoding("utf-8");

                   PrintWriter out = response.getWriter();

                  

                   String username = request.getParameter("username");

                   String password = request.getParameter("password");

                  

                   //如果登陆成功

                   if(username != null && username.equals("管理员") && password != null && password.equals("123456")){

                            request.getSession().setAttribute("username", username);

                            request.getRequestDispatcher("main.jsp").forward(request, response);

                   }else{

                            response.sendRedirect("index.jsp?msg=error");

                   }

 

                  

                   out.flush();

                   out.close();

         }

 

}

 

main.jsp

        欢迎<%=session.getAttribute("username") %>访问我们的系统!<br>

 

LoginFilter

/**

 * 判断用户是否登陆的过滤器

 */

public class LoginFilter implements Filter{

 

         public void destroy() {

                   System.out.println("LoginFilter被销毁!");

         }

 

         public void doFilter(ServletRequest req, ServletResponse res,

                            FilterChain chain) throws IOException, ServletException {

                   HttpServletRequest request = (HttpServletRequest) req;//ServletRequest转换为HttpServletRequest

                   HttpServletResponse response = (HttpServletResponse) res;

                  

                   System.out.println("LoginFilter调用成功!");

                   //用数组存储不需要登陆就能访问的页面

                   String[] pages = {"index.jsp","reg.jsp","aboutus.jsp"};

                  

                   //获取用户要访问的页面 /testfilter/index.jsp

                   String uri = request.getRequestURI();

                   //System.out.println(uri);

                  

                   //如果用户要访问的页面是必须登陆的页面

                   if(needLogin(uri,pages)){

                            //判断用户是否登陆

                            if(request.getSession().getAttribute("username") == null){

                                     response.sendRedirect("index.jsp?msg=notlogin");

                            }else{

                                     chain.doFilter(req, res);

                            }

                   }else{

                            //filter放行

                            chain.doFilter(req, res);

                   }

                  

                  

         }

        

         /**

          * 判断用户想要访问的页面是否是需要登陆才能访问的页面

          * 这个方法应该定义在Bean中,此处为了方便定义在这里

          * @param uri 用户将要访问的页面

          * @param pages 用户不需要登陆就能访问的页面

          * @return

          */

         public boolean needLogin(String uri,String[] pages){

                   boolean r = true;//假设需要登陆才能访问

                  

                   //循环不需要登陆的列表,看看用户想访问的页面是否在列表中

                   if(uri != null && pages != null){

                            for(int i = 0;i < pages.length;i++){

                                     //如果我们想要访问的页面,包含了pages[i],说明不需要登陆

                                     if(uri.contains("/" + pages[i])){

                                               r = false;

                                               break;

                                     }

                            }

                   }

                  

                   return r;

         }

 

         public void init(FilterConfig arg0) throws ServletException {

                   System.out.println("LoginFilter初始化!");

         }

 

}

 

web.xml

  <servlet>

    <servlet-name>LoginServlet</servlet-name>

    <servlet-class>com.user.servlet.LoginServlet</servlet-class>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>LoginServlet</servlet-name>

    <url-pattern>/LoginServlet</url-pattern>

  </servlet-mapping>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <!-- 配置过滤器 -->

  <!-- 声明节点 -->

  <filter>

    <filter-name>LoginF</filter-name>

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

  </filter>

 

  <!-- 映射节点 -->

  <filter-mapping>

     <filter-name>LoginF</filter-name>

   <!—过滤所有的jsp访问 -->

     <url-pattern>*.jsp</url-pattern>

  </filter-mapping>


Powered by 东方博宜教育咨询江苏有限公司  ©2008-2017 www.czos.cn