JAVAWeb - Filter(过滤器)
  TEZNKK3IfmPf 2023年11月12日 29 0

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

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

过滤器的分析:

我们用户在使用web浏览器访问web服务器的时候;以往的过程都是web服务器直接去寻址拿资源(servlet,jsp,HTML,静态资源),然而在请求处理的时候,我们通常会因为请求和响应的数据结构类型不同,导致网页出现乱码,或者崩溃的状态。

我们在web服务器和静态资源中加了一层过滤器,专门对请求和响应的码进行处理,统一规格化请求和响应的代码,就可以避免乱码的情况。

一:导包

  <dependencies>
        <!--Servlet  的依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp的依赖-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <!--jstl 表达式的依赖-->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- taglibs 标签库 -->
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

一般在maven中导入这四个包就可以完成了,过滤器一般的实现只需要这四个包

二:编写过滤器

导包:java.servlet

实现Filter接口:

注意点:过滤中一定要包含这个方法   filterChain.doFilter  不然请求就会停在监听器中

public class CharacterEncodingFilteer implements Filter {
    //初始化:在web服务器启动的时候就已经初始化完成了
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("CharacterEncodingFilter已经初始化");
    }
//chain
    /*
    1.过滤器中的代码,在过滤特定需求的请求中都会执行
    2.必须要让过滤器往下进行:filterChain.doFilter
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=UTF-8");
        System.out.println("CharacterEncodingFilter执行前");
        filterChain.doFilter(servletRequest,servletResponse);//让我们的请求继续向下交接;没有这句话就会停在这个过滤器内
        System.out.println("CharacterEncodingFilter执行后");
    }
//销毁:在web服务器关闭的时候过滤器就会自动关闭
    public void destroy() {
        System.out.println("CharacterEncodingFilter已经销毁");
    }
}

  在web.xml中配置Filter的作用域

  <filter>
    <filter-name>CharacterEncodingFilteer</filter-name>
    <filter-class>top.lostyou.jsp.Filter.CharacterEncodingFilteer</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilteer</filter-name>
    <!-- 过滤Filter包下的文件的请求-->
    <url-pattern>/Filter/*</url-pattern>
  </filter-mapping>

监听器:实现一个监听器的接口(很多)

1.编写一个监听器:实现监听器的接口

//统计在线人数:统计session
public class OnlineCountLinster implements HttpSessionListener {
    //创建session的监听
    //一旦创建一个session就会触发一次这个事件
    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就会触发一次这个事件
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext ctx = se.getSession().getServletContext();
        System.out.println(se.getSession().getId());
        se.getSession().invalidate();
        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);
    }
}

 2.在web.xml中注册监听器,并设置session的过期事件

  <!--注册监听器-->
  <listener>
    <listener-class>top.lostyou.jsp.linster.OnlineCountLinster</listener-class>
  </listener>
  <session-config>
    <session-timeout>1</session-timeout>
  </session-config>

过滤器和监听器的常见应用:

监听器GUI的实现:

public class TestPanel  {
    public static void main(String[] args) {
        Frame frame = new Frame("监听器GUI测试");//新建一个窗体
        Panel panel = new Panel(null);//面板
        frame.setLayout(null);//设置窗体布局
        frame.setBounds(300,300,500,500);//设置窗体位置,及显示大小
        frame.setBackground(new Color(0,0,255));//设置背景颜色
        panel.setBounds(50,50,300,300);
        panel.setBackground(new Color(0,255,0));
        frame.add(panel);
        frame.setVisible(true);//设置窗体可视化
        //监听事件,关闭监听事件
        frame.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {
                System.out.println("打开");
            }

            public void windowClosing(WindowEvent e) {
                System.out.println("关闭ing");
                System.exit(0);
            }

            public void windowClosed(WindowEvent e) {
                System.out.println("关闭ed");
            }

            public void windowIconified(WindowEvent e) {

            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {
                System.out.println("激活");
            }

            public void windowDeactivated(WindowEvent e) {
                System.out.println("未激活");
            }
        });

    }
}

 以上通过GUI的方法实现窗口:

在运行以后一个窗体被呈现了出来

此时我们对窗体的操作都使用监听器去监听;

每当我们执行一个事件都可能会有反馈,就是监听器的作用。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   17天前   18   0   0 JavaSE
  TEZNKK3IfmPf   17天前   40   0   0 java
  TEZNKK3IfmPf   2024年05月31日   51   0   0 java
TEZNKK3IfmPf