实现登陆功能:
登陆页面(用户名和密码要能传到后台)index.jsp
servlet接收数据–>数据传递给service–>service经过合法性判断–>调用dao的查询方法–>dao查询返回数据给service–>service经过业务逻辑判断–>把结果传回给servlet–>传回给页面
8.25课前复习:
1、jdbc连接数据库的两种步骤
加载驱动:Class.forName(“com.mysql.jdbc.Driver”);
获取连接:Connection conn=DriverManager.getConnection(url,user,password)
statement:
创建语句管理对象:Statement stmt=conn.getStatement();
装载sql并运行:
ResultSet rs=stmt.executeQuery(sql);
int num=stmt.executeUpdate(sql);
PreparedStatement
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
int num=pst.executeUpdate();
遍历结果集rs
关闭资源
2、数据库配置使用配置文件存储的方式
Properties p=new Properties();
p.load(new FileInputStream(配置文件路径));
String 属性名 =p.get(属性名);
3、Java三层模型是指哪三层分别什么作用
dao:与数据库交互
service:业务逻辑的判断
controller:与前端页面的交互
配置web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>cn.kgc.kb09.controller.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<!--/login.html /不要丢-->
<url-pattern>/login.html</url-pattern>
</servlet-mapping>
</web-app>
初始页面index.jsp:
<%--
Created by IntelliJ IDEA.
User: chaokeaimuzhi
Date: 2020/8/22
Time: 14:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>欢迎来到吃货联盟</h1>
<form action="login.html" method="get">
<p><input type="text" name="username"></p>
<p><input type="password" name="password"></p>
<p><input type="submit" name="点我登陆"></p>
</form>
</body>
</html>
cn.kgc.kb09.controller.LoginServlet: