当前位置: 首页 > news >正文

JavaWeb基础五(MVC)

什么是MVC:Model View Controller 模型,视图,控制器

以前

Servlet和JSP都可以写Java代码,为了易于维护和使用;Servlet专注于处理请求,以及控制视图跳转;JSP专注于显示数据;

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

servlet--CRUD-->数据库

弊端:程序十分臃肿,不利于维护

servlet的代码中:处理请求、响应、视图跳转、处理JDBC、处理业务代码,处理逻辑代码

架构:加一层

程序员调用

|

JDBC

|

Mysql、Oracle、Sqlserver(不同数据库)

三层架构

Model

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

View

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

Controller(Servlet)

  • 接收用户的请求(req:请求参数、Session信息...)
  • 交给业务层处理对应的代码
  • 控制视图的跳转

登录-->接收用户的登录请求-->处理用户的请求(获取用户登录的参数,username、password)-->交给业务层处理登录业务(判断用户名密码是否正确:事务)-->Dao层查询用户名和密码是否正确-->数据库

Filter过滤器(重点)

用来过滤网站的数据;

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

开发步骤:导入依赖

<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>

编写过滤器

  • 导包

  • 编写过滤器

配置过滤器

编写servlet代码测试:

监听器listener

实现一个监听器的接口;有很多种

编写一个监听器,实现监听器的接口。

//统计网站在线人数,统计Session
public class OnlineCountListener implements HttpSessionListener {
    //创建session监听,看你的一举一动
    //一旦创建Session就会触发一次这个事件
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        ServletContext ctx = httpSessionEvent.getSession().getServletContext();
        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 httpSessionEvent) {
        ServletContext ctx = httpSessionEvent.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);
    }
}

在web.xml中注册监听器

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

设置一个jsp页面

 <body>
<h1>当前有<span><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%></span>人在线</h1>
  </body>

相关文章:

  • matlab数值精度就1e15
  • 基于Spring Boot的民宿租赁系统的设计与实现(LW+源码+讲解)
  • Maxscript如何通过单击现有按钮添加新按钮?
  • Leetcode做题记录----3
  • 如何在Linux中切换用户?
  • Linux--进程优先级
  • docker部署sflow-rt使用
  • PHP与MySQL的高效数据交互:最佳实践与优化技巧
  • java 中判断对象是否可以被回收和 GCROOT
  • 每日Attention学习25——Multi-Scale Attention Fusion
  • 结构体1~5(1414. 期末考试成绩排名、1490. 坐标排序、1315. 遥控飞机争夺赛、1730. 购买贺年卡、1499. 宇宙总统2)
  • Windows Qt动态监测系统分辨率及缩放比变化
  • LGA封装 Z3588开发板,8K视频编解码
  • 设计模式使用Java案例
  • 《AI大模型趣味实战》No2 : 快速搭建一个漂亮的AI家庭网站-相册/时间线/日历/多用户/个性化配色(中)
  • Leetcode-131.Palindrome Partitioning [C++][Java]
  • RUOYI框架在实际项目中的应用三:Ruoyi微服务版本-RuoYi-Cloud
  • JAVA数据库技术(一)
  • Deepseek学习--工具篇之Ollama
  • 基于C#的以太网通讯实现:TcpClient异步通讯详解
  • 港股上市首日大涨,宁德时代“新动力”何在?曾毓群详谈零碳科技布局
  • 这款小孩子最爱的饮料,害处竟大到不敢想象
  • 上海文化馆服务宣传周启动,为市民提供近2000项活动
  • 台陆委会将欧阳娜娜等20多名艺人列入重要查核对象,国台办回应
  • 揭秘拜登退选内幕新书引争议,“垃圾信息在四处传播”?
  • AI快速迭代带来知识焦虑,褚君浩院士提出“四维能力模型”