JavaWeb -JavaBean MVC Filter 监听器 过滤器

目录

JavaBean

MVC三层架构

Filter(重点)

监听器

过滤器,监听器的常见应用


JavaBean

实体类

JavaBean有特定的写法:

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须有对应的get/set方法

一般来说和数据库的字段做映射 ORM

ORM:对象关系映射

  • 表--->类
  • 字段--->属性
  • 行记录--->对象

id

name

age

address

1

饼干1号

11

河南

2

饼干2号

12

河南

3

饼干3号

13

河南

class People{
    private int id;
    private String name;
    private int age;
    private String address;
}
class A{
    new People(1,"饼干1号",11,"河南");
    new People(2,"饼干2号",12,"河南");
    new People(3,"饼干3号",13,"河南");
}

MVC三层架构

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

之前

Servlet--CRUD-->数据库

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

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

架构:没有什么是加一层解决不了的

MVC三层架构

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

Filter(重点)

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

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

1.导包

2.编写过滤器

3.在web.xml中配置Filter

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

监听器

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

1.编写一个监听器

实现一个监听器接口

//统计网站在线人数:统计session
public class OnlineCounterListener implements HttpSessionListener{
//创建Session监听:看你的一举一动
//一旦创建Session就会触发一次这个事件
public void sessionCreated(HttpSessionEvent se){
    ServletContext ctx = se.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 se){
    ServletContext ctx = se.getSession().getServletContext();
    Integer onlineCount = (Integer) ctx.getAttribute(""onlineCount);
    if(onlineCount == null){
        onlineCount = new Integer();
    }else{
        int count = onlineCount.intValue(1);
        onlineCount = new Integer(count-1);
    }
    ctx.setAttribute("OnlineCount",onlineCount);
}
}

/*
session销毁
1.手动销毁 getSession().invalidate();
2.手动销毁
*/

过滤器,监听器的常见应用

监听器:GUI编程中经常使用

GUI:图形界面编程

package com.he.listener;
import  java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

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);
        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("关闭中");
                System.exit(0);
            }

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

            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("未激活");
            }
        });
    }
}

用户登录之后才能进入主页,用户注销后就不能进入主页了

1.用户登录之后,向Session中放入用户的数据

2.进入主页的时候要判断用户是否以及登录,要求:在过滤器中实现

HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse reponse = (HttpServletResponse)resp;

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

chain.doFilter(request,reponse);

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级小何

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值