首页 > 其他分享 >Filter+Listener

Filter+Listener

时间:2023-03-14 09:14:40浏览次数:47  
标签:Listener com void javax Filter import servlet public

11.Filter (重点)

Filter:过滤器 ,用来过滤网站的数据;

  • 处理中文乱码
  • 登录验证….

1568424858708

Filter开发步骤:

  1. 导包

  2. 编写过滤器

    1. 导包不要错;

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>JavaWeb-02-Servlet</artifactId>
              <groupId>com.github</groupId>
              <version>1.0-SNAPSHOT</version>
          </parent>
          <modelVersion>4.0.0</modelVersion>
      
          <artifactId>Filer</artifactId>
      
          <dependencies>
              <!--   Servlet 依赖   -->
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>servlet-api</artifactId>
                  <version>2.5</version>
              </dependency>
              <!--        JSP 依赖   -->
              <dependency>
                  <groupId>javax.servlet.jsp</groupId>
                  <artifactId>javax.servlet.jsp-api</artifactId>
                  <version>2.3.3</version>
              </dependency>
              <!--        JSTL表达式的依赖-->
              <dependency>
                  <groupId>javax.servlet.jsp.jstl</groupId>
                  <artifactId>jstl-api</artifactId>
                  <version>1.2</version>
              </dependency>
              <!--        standard标签库-->
              <dependency>
                  <groupId>taglibs</groupId>
                  <artifactId>standard</artifactId>
                  <version>1.1.2</version>
              </dependency>
              <!--    连接数据库-->
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>5.1.47</version>
              </dependency>
          </dependencies>
      
      </project>
      

      image-20210802171522721

    2. 实现Filter接口,重写对应的方法即可;

      package com.github.filter;
      
      import javax.servlet.*;
      import java.io.IOException;
      
      public class CharacterEncodingFilter implements Filter {
          
          /**
           * 初始化:web服务器启动,就以及初始化了,随时等待过滤对象出现!
           */
          public void init(FilterConfig filterConfig) {
              System.out.println("CharacterEncodingFilter初始化");
          }
      
          /**
           * Chain : 链
           * 
           * 1. 过滤中的所有代码,在过滤特定请求的时候都会执行
           * 2. 必须要让过滤器继续同行
           *    chain.doFilter(request,response);
           */
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
              request.setCharacterEncoding("utf-8");
              response.setCharacterEncoding("utf-8");
              response.setContentType("text/html;charset=UTF-8");
      
              System.out.println("CharacterEncodingFilter执行前....");
              // 让我们的请求继续走,如果不写,程序到这里就被拦截停止!
              chain.doFilter(request,response);
              System.out.println("CharacterEncodingFilter执行后....");
          }
      
          /**
           * 销毁:web服务器关闭的时候,过滤会销毁
           */
          public void destroy() {
              System.out.println("CharacterEncodingFilter销毁");
          }
      }
      
  3. 在web.xml中配置 Filter;

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.github.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <!--只要是 /servlet的任何请求,会经过这个过滤器-->
            <url-pattern>/servlet/*</url-pattern>
            <!--<url-pattern>/*</url-pattern>-->
        </filter-mapping>
    
    </web-app>
    

12.监听器

实现一个监听器的接口;(有N种)

  1. 编写一个监听器;

    实现监听器的接口…

    package com.github.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * @Description: 统计网站在线人数 : 统计session
     */
    public class OnlineCountListener implements HttpSessionListener {
        /**
         * 创建session监听: 看你的一举一动
         * 一旦创建Session就会触发一次这个事件!
         * @param se
         */
        public void sessionCreated(HttpSessionEvent se) {
            ServletContext ctx = se.getSession().getServletContext();
    
            System.out.println(se.getSession().getId());
    
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
    
            if (onlineCount==null){
                onlineCount = new Integer(1);
            }else {
                int count = onlineCount.intValue();
                onlineCount = new Integer(count+1);
            }
    
            ctx.setAttribute("OnlineCount",onlineCount);
        }
    
        /**
         * 销毁session监听
         * 一旦销毁Session就会触发一次这个事件!
         * @param se
         */
        public void sessionDestroyed(HttpSessionEvent se) {
            ServletContext ctx = se.getSession().getServletContext();
    
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
    
            if (onlineCount==null){
                onlineCount = new Integer(0);
            }else {
                int count = onlineCount.intValue();
                onlineCount = new Integer(count-1);
            }
    
            ctx.setAttribute("OnlineCount",onlineCount);
        }
        /**
         * Session销毁:
         * 1. 手动销毁  getSession().invalidate();
         * 2. 自动销毁
         */
    }
    
  2. web.xml中注册监听器;

    <!--注册监听器-->
    <listener>
        <listener-class>com.github.listener.OnlineCountListener</listener-class>
    </listener>
    
  3. 看情况是否使用!

13.过滤器.监听器常见应用

  • 监听器:GUI编程中经常使用;
package com.github.listener;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestPanel {
    public static void main(String[] args) {
        // 新建一个窗体
        Frame frame = new Frame("建军节快乐");  
        // 面板
        Panel panel = new Panel(null);
        // 设置窗体的布局
        frame.setLayout(null); 

        frame.setBounds(300,300,500,500);
        // 设置背景颜色1
        frame.setBackground(new Color(68, 227, 177)); 

        panel.setBounds(50,50,300,300);
        // 设置背景颜色2
        panel.setBackground(new Color(255, 242,0)); 

        frame.add(panel);

        frame.setVisible(true);

        // 监听事件,监听关闭事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
            }
        });

    }
}

案例:用户登录之后才能进入主页!用户注销后就不能进入主页了!

  1. 用户登录之后,向Sesison中放入用户的数据
  2. 进入主页的时候要判断用户是否已经登录;要求:在过滤器中实现!
  • 因为:个人tomcat配置的为 /Filer 如下图:

image-20210803212625696

  • LoginServlet.java
package com.github.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        super.doGet(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端请求的参数
        String username = req.getParameter("username");

        // 登陆成功
        if("admin".equals(username)){
            req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
            resp.sendRedirect("/Filer/sys/success.jsp");

        } else {    // 登陆失败
            resp.sendRedirect("/Filer/error.jsp");
        }
    }
}
  • LogoutServlet.java
package com.github.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user_session = req.getSession().getAttribute("USER_SESSION");
        if(user_session!=null){
            req.getSession().removeAttribute("USER_SESSION");
            resp.sendRedirect("/Filer/Login.jsp");
        } else {
            resp.sendRedirect("/Filer/Login.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req,resp);
    }
}
  • Login.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Login</title>
</head>
<body>

<h1>登陆界面</h1>
<form action="/Filer/servlet/login" method="post">
    <input type="text" name="username">
    <input type="submit">
</form>

</body>
</html>
  • success.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>成功</title>
</head>
<body>

<%--<%--%>
<%--    Object userSession = request.getSession().getAttribute("USER_SESSION");--%>
<%--    if(userSession==null){--%>
<%--        response.sendRedirect("Filer/Login.jsp");--%>
<%--    }--%>
<%--%>--%>

<h1>主页</h1>

<p><a href="/Filer/servlet/logout">注销</a> </p>

</body>
</html>
  • error.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>error</title>
</head>
<body>

<h1>错误</h1>
<h3>没有权限,用户名错误</h3>

<p> <a href="/Filer/Login.jsp">返回登录主页</a></p>

</body>
</html>
  • 进行登录注销无法登录判断;
  • Constant.java
package com.github.Util;

public class Constant {
    public static String USER_SESSION="USER_SESSION";
}
  • SysFilter.java
package com.github.listener;

import com.github.Util.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        if (request.getSession().getAttribute(Constant.USER_SESSION)==null){
            response.sendRedirect("/Filer/error.jsp");
        }

        chain.doFilter(request,response);
    }

    public void destroy() {

    }
}
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.github.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/servlet/login</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.github.servlet.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>/servlet/logout</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>SysFilter</filter-name>
        <filter-class>com.github.listener.SysFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SysFilter</filter-name>
        <url-pattern>/sys/*</url-pattern>
    </filter-mapping>
    
</web-app>
  • 运行结果如下:

login

标签:Listener,com,void,javax,Filter,import,servlet,public
From: https://www.cnblogs.com/qqingniao/p/17213650.html

相关文章