一、前戏

用户直接访问控制层,控制层就可以直接操作数据库;
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:过滤器,用来过滤网站的数据;
- 处理中文乱码
- 登录验证….

CharacterEncodingFilter.java
// 初始化public void init(FilterConfig filterConfig) throws ServletException {System.out.println("CharacterEncodingFilter成功激活!");}// filterChain:链/** 1.过滤中的所有代码,在过滤特定请求的时候都会执行* 2.必须要让过滤器继续通行*/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>CharacterEncodingFilter</filter-name><filter-class>com.shuai.filter.CharacterEncodingFilter</filter-class></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><!--只要是 /servlet的任何请求,会经过这个过滤器--><url-pattern>/servlet/*</url-pattern><!--<url-pattern>/*</url-pattern>--></filter-mapping>
三、监听器
实现一个监听器的接口:(有N种)
编写一个监听器
实现监听器的接口....
// 统计网站在线人数,统计sessionpublic class OnlineCountListener 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();System.out.println(se.getSession().getId());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);}}
index.jsp
<h1>当前有 <span style="color: red"><%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%></span> 人在线</h1>
2.销毁监听器
// 手动销毁se.getSession().invalidate();// 自动注销<session-config><session-timeout>1</session-timeout></session-config>
监听器:GUI编程中经常使用;
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);// rgb ---> 红绿蓝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("未激活");}});}}
