Day46-MVC三层架构、Filter过滤器、监听器

Day46-MVC三层架构、Filter过滤器、监听器

MVC三层架构

什么是MVC:Model view Controlller 模型 视图 控制器

早些年的框架

在这里插入图片描述

用户直接访问控制层,控制层就可以直接操作数据库:

servlet》CRUD》数据库
弊端:程序十分臃肿,不利于维护
servlet的代码中要解决:  处理请求、响应、视图跳转、处理JDBC、处理业务代码、处理逻辑代码
架构:没有什么是加一层解决不了的!
程序员》JDBC》Mysql  Oracle  SqlServer

三层架构

在这里插入图片描述

Model

  • 业务处理:业务逻辑(Service)
  • 数据持久层:CRUD(Dao)

View

  • 展示数据
  • 提供链接发起Servlet请求(a,form,img)

Controller(Servlet)

  • 接收用户的请求(req:请求参数、Session信息…)
  • 交给业务层处理对应的代码
  • 控制视图的跳转
登录》接收用户的登录请求》处理用户的请求(获取用户登录的参数,username,password)》交给业务层处理登录业务(判断用户名密码是否正确:事务)》Dao层查询用户名和密码是否正确》数据库

Filter过滤器

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

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

在这里插入图片描述

Filter开发步骤

  1. 导包

  2. 编写过滤器

    1. 导包(一定要是servlet下的

    在这里插入图片描述

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

    CharacterEncodingFilter.java

    package com.xiaozhao.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    /**
     * @author 小龚
     * @create 2022-09-07 21:36
     */
    //字符编码过滤器
    public class CharacterEncodingFilter implements Filter {
        //初始化:web服务器启动,就已经初始化了,随时等待过滤器对象出现。
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("初始化");
        }
        //Chai:链的意思
        /*
        1.过滤器的所有代码,在过滤器特定请求的时候都会执行
        2.必须要让过滤器继续往下转交
        固定代码:filterChain.doFilter(servletRequest,servletResponse);
        * */
        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("执行前");
            filterChain.doFilter(servletRequest,servletResponse);//让我们的请求继续走,如果不写,程序到这里就被拦截停止了
            System.out.println("执行后");
    
        }
        //销毁:web服务器关闭的时候,过滤器会销毁
        public void destroy() {
            System.out.println("销毁");
        }
    }
    

    ShowServlet.java

    package com.xiaozhao.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * @author 小龚
     * @create 2022-09-07 21:48
     */
    public class ShowServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("我爱小赵");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    1. 配置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>ShowServlet</servlet-name>
              <servlet-class>com.xiaozhao.servlet.ShowServlet</servlet-class>
          </servlet>
          <servlet-mapping>
              <servlet-name>ShowServlet</servlet-name>
              <url-pattern>/servlet/show</url-pattern>
          </servlet-mapping>
          <servlet-mapping>
              <servlet-name>ShowServlet</servlet-name>
              <url-pattern>/show</url-pattern>
          </servlet-mapping>
          <filter>
              <filter-name>CharacterEncodingFilter</filter-name>
              <filter-class>com.xiaozhao.filter.CharacterEncodingFilter</filter-class>
          </filter>
          <filter-mapping>
              <filter-name>CharacterEncodingFilter</filter-name>
              <!--只要是/servlet目录下的任何请求,都会经过这个过滤器-->
              <url-pattern>/servlet/*</url-pattern>
          </filter-mapping>
      </web-app>
      

测试:

没通过过滤器的

在这里插入图片描述

没通过过滤器的

在这里插入图片描述

监听器

实现一个监听器的接口(多种)(看情况是否使用)

  1. 实现一个监听器

    实现监听器的接口

    package com.xiaozhao.listener;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    
    /**
     * @author 小龚
     * @create 2022-09-07 22:54
     */
    //在线人数监听,统计网站在线人数(统计session)
    public class OnlineCountListener implements HttpSessionListener {
        //创建session监听
        //一旦创建session就会触发这个事件
        public void sessionCreated(HttpSessionEvent se) {
            ServletContext ctx = se.getSession().getServletContext();
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
            //如果为空onlineCount+1,不为空则count+1
            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();
            Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
            //如果为空onlineCount就为0个人,不为空则count-1
            if(onlineCount==null){
                onlineCount = new Integer(0);
            }else{
                int count = onlineCount.intValue();
                onlineCount = new Integer(count-1);
            }
            ctx.setAttribute("OnlineCount",onlineCount);
        }
        /*
        Session销毁:
        1.手动销毁 se.getSession().invalidate();
        2.自动销毁 通过配置web.xml
        * */
    }
    
  2. web.xml中注册监听器

        <!--注册监听器-->
        <listener>
            <listener-class>com.xiaozhao.listener.OnlineCountListener</listener-class>
        </listener>
    

测试

index.jsp

<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h1>当前有<span style="color:blue;"><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%></span>人在线</h1>
</body>
</html>

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值