JavaBean、MVC架构简介、Filter、listener

1.JavaBean

1.作用:

将java对象变为脚本对象。JavaBean 组件可以用来执行复杂的计算任务,或负责与数据库的交互以及数据提取等。如果我们有三个 JavaBean ,它们分别具有显示新闻、股票价格、天气情况的功能,则创建包含所有这三种功能的 Web 页面只需要实例化这三个 Bean ,使用 HTML 表格将它们依次定位就可以了。

2.实体类,JavaBean有特定的写法:

  • 必须要有无参构造
  • 属性必须私有化
  • 必须有对应的set、get方法
  • 需要被序列化并且实现了 Serializable 接口

一般用来和数据库做映射

3.ORM(对象关系映射)

数据库 Java
属性 属性
元祖 对象

4.使用:

创建people类,设置私有属性,实现Serializable 接口,设置get、set方法,set方法可从数据库中获取数据进行set,get可以在jsp中使用<%=people.getid()%>获取,也可以使用JavaBean

<jsp:useBean id="people" class="com.Gw.pojo.People" scope="page"></jsp:useBean>
<jsp:setProperty name="people" property="address" value="成都"></jsp:setProperty>
<jsp:setProperty name="people" property="age" value="20"></jsp:setProperty>
<jsp:setProperty name="people" property="name" value="ygw"></jsp:setProperty>
<jsp:setProperty name="people" property="id" value="1"></jsp:setProperty>


<jsp:getProperty name="people" property="name"/>
<jsp:getProperty name="people" property="id"/>
<jsp:getProperty name="people" property="age"/>
<jsp:getProperty name="people" property="address"/>

2.MVC三层架构

什么是MVC:model-view-controller(模型-视图-控制器)

image

3.过滤器Filter

1.作用:

  • 用来过滤网站的数据;web服务器中有一些垃圾请求,后台不应该处理或者应该报错

  • 处理中文乱码

  • 登录验证

image

2.使用步骤

1.导包(Filter 与Servlet类似)

            <!-- servlet的依赖-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>

            <!--jsp的依赖-->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.3</version>
                <scope>provided</scope>
            </dependency>

2.实现接口(javax.servlet.Filter)

public class CharacterEncodingFilter implements Filter {
    //初始化
    public void init(FilterConfig filterConfig) throws ServletException {
        //服务器开始的时候就已经启动,等待过滤对象的出现
        //能在规律的时候放入一些属性
//        filterConfig.getServletContext().setAttribute();

        System.out.println("init");
    }

    //chain:链
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("gbk");
        servletResponse.setCharacterEncoding("gbk");
        servletResponse.setContentType("text/html; charset = gbk");

        System.out.println("Character Encoding Filter 执行前...");
        filterChain.doFilter(servletRequest, servletResponse);
        //让程序继续走,如果不写,程序到这里就停止了
        //允许程序有多个过滤器
        System.out.println("Character Encoding Filter 执行后...");

    }

    //销毁
    public void destroy() {
        System.gc();
        //web服务器关闭的时候过滤器会销毁
        System.out.println("destroy");
    }
}

3.在web.xml中注册

        <filter>
            <filter-name>CharacterEncodingFilter</filter-name>
            <filter-class>com.Gw.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/servlet/*</url-pattern>
        </filter-mapping>

4.监听器

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

package com.Gw.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineCountListener implements HttpSessionListener {
    //创建session监听每创建一个触发一次
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        Integer onlineCount = (Integer)servletContext.getAttribute("OnlineCount");
        if(onlineCount == null){
            onlineCount = new Integer(1);
        }else{
            int count = onlineCount.intValue();
            onlineCount = new Integer(count + 1);
        }
        servletContext.setAttribute("OnlineCount", onlineCount);
    }
    //每销毁一个触发一次
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext servletContext = se.getSession().getServletContext();
        Integer onlineCount = (Integer)servletContext.getAttribute("OnlineCount");
        if(onlineCount == null){
            onlineCount = new Integer(0);
        }else{
            int count = onlineCount.intValue();
            onlineCount = new Integer(count - 1);
        }
        servletContext.setAttribute("OnlineCount", onlineCount);
    }
}

2.在web中注册

        <listener>
            <listener-class>com.Gw.listener.OnlineCountListener</listener-class>
        </listener>
posted @ 2022-03-28 20:35  CDUT的一只小菜鸡  阅读(48)  评论(0编辑  收藏  举报