10、MVC三层架构
什么是MVC:Model View Controller 模型、视图、控制器
10.1、早些年
10.2、MVC三层架构
11、Filter(过滤器)重点
Filter:过滤器,用来过滤网站的数据;
-
处理中文乱码
-
登录验证。。。
Filter开发步骤:
-
导包
-
编写过滤器
导包不要错
实现Filter接口,重写对应的方法即可
public class CharacterEncoding implements Filter{
//初始化:web服务器启动,就已经初始化了,随时等待过滤对象出现!
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncoding初始化");
}
//Chain:链
/*
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");
System.out.println("CharacterEncoding执行前...");
filterChain.doFilter(servletRequest,servletResponse);//让我们的请求继续走,如果不写,程序到这里就被拦截停止!
System.out.println("CharacterEncoding执行后...");
}
//销毁:web服务器关闭的时候,过滤会销毁
public void destroy() {
System.out.println("CharacterEncoding销毁");
}
}
-
在web.xml中配置Filter
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>com.kuang.filter.CharacterEncoding</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<!-- 只要是/servlet的任何请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
12、监听器
实现一个监听器的接口;(有N种)
-
编写一个监听器
//统计网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
//创建session监听:看你的一举一动
//一旦创建Session就会触发一次这个事件!
public void sessionCreated(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(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(0);
}else {
int count = onlineCount.intValue();
onlineCount = new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
/*
Session销毁
1.手动销毁 getSession().invalidate();
2.自动销毁
*/
}
-
web.xml中注册监听器
<!--注册监听器-->
<listener>
<listener-class>com.kuang.listener.OnlineCountListener</listener-class>
</listener>
3.看情况是否使用!
13、过滤器、监听器常见应用
监听器: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);
//这只背景颜色
frame.setBackground(Color.red);
panel.setBounds(50,50,300,300);
panel.setBackground(Color.blue);
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭了");
System.exit(0);
}
});
}
}
用户登录之后才能进入主页!用户注销之后就不能进入主页了!
-
用户登录之后,向Session中放入用户的数据
-
进入主页的时候要判断用户是否已经登录;要求:在过滤器中实现!
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
//ServletRequest HttpServletRequest
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if (request.getSession().getAttribute(Constant.user_session) == null) {
response.sendRedirect("/error.jsp");
}
filterChain.doFilter(req, resp);
}
14、JDBC
什么是JDBC:Java连接数据库!
需要jar包的支持:
-
java.sql
-
javax.sql
-
mysql-connector-java... 连接驱动(必须导入)
实验环境搭建
CREATE TABLE IF NOT EXISTS `users`(
id INT(20) PRIMARY KEY,
`name` VARCHAR(40),
`password` VARCHAR(40),
birthday DATE
)ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO users(id,`name`,`password`,email,birthday)VALUES(1,'张三','123456','zgr@qq.com','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday)VALUES(2,'李四','123456','ls@qq.com','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday)VALUES(3,'王五','123456','ww@qq.com','2000-01-01');
INSERT INTO users(id,`name`,`password`,email,birthday)VALUES(4,'赵6','123456','zl@qq.com','2000-01-01')
导入数据库依赖
<!-- mysql的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
IDEA中连接数据库
JDBA固定步骤:
-
加载驱动
-
连接数据库,代表数据库
-
向数据库发送SQL的对象Statement:CRUD
-
编写SQL(根据业务,不同的SQL)
-
执行SQL
-
关闭连接
public class TestJdbc {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//配置信息
//useUnicode=true&characterEncoding=utf8解决中文乱码
String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "123456";
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url,username,password);
//3.向数据库发送SQL的对象Statement:CRUD
Statement statement = connection.createStatement();
//4.编写SQL
String sql =" select * from users";
//5.执行SQL
ResultSet rs = statement.executeQuery(sql);
while (rs.next()){
System.out.println("id="+rs.getObject("id"));
System.out.println("name="+rs.getObject("name"));
System.out.println("password="+rs.getObject("password"));
System.out.println("email="+rs.getObject("email"));
System.out.println("birthday="+rs.getObject("birthday"));
}
//关闭连接,释放资源(一定要做)
rs.close();
statement.close();
connection.close();
}
}
预编译SQL
Junit单元测试
依赖
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
简单使用
@Test注解只有在方法上有效,只要加了这个注解的方法,就可以直接运行!