一、准备数据库
二、实现步骤
三、测试效果
代码下载
一、数据库
数据库:web
表:user
字段 | 类型 |
id | int |
username | varchar |
password | varchar |
![db98ab31d54839ece2937c43543f488e.png](https://img-blog.csdnimg.cn/img_convert/db98ab31d54839ece2937c43543f488e.png)
![94747046853feaf6ce588d0b50fe243b.png](https://img-blog.csdnimg.cn/img_convert/94747046853feaf6ce588d0b50fe243b.png)
二、新建项目
MVC分层架构
模型(model)-视图(view)-控制器(controller)
src 放后端代码,webcontent放前端代码,lib放第三方jar包
![2e6d4c4ad51fd936f2d8d0ee7976ab84.png](https://img-blog.csdnimg.cn/img_convert/2e6d4c4ad51fd936f2d8d0ee7976ab84.png)
![e21312af374a1fc471a69fc1050cb7e9.png](https://img-blog.csdnimg.cn/img_convert/e21312af374a1fc471a69fc1050cb7e9.png)
关键代码说明
Index.jsp
![0e11286b8d48d3b3d30ee355f85eda2f.png](https://img-blog.csdnimg.cn/img_convert/0e11286b8d48d3b3d30ee355f85eda2f.png)
LogServlet.java
![27cb59c5aa77a86b37e155cf519fe99f.png](https://img-blog.csdnimg.cn/img_convert/27cb59c5aa77a86b37e155cf519fe99f.png)
servlet起名规则一般是功能+Servlet,单词首字母大写。
![0b95e63787f46f01971a5860bc204712.png](https://img-blog.csdnimg.cn/img_convert/0b95e63787f46f01971a5860bc204712.png)
这个@WebServlet()里的是这个servlet的访问路径名。
![ba814fe6d128cb1be35875e1672da8ea.png](https://img-blog.csdnimg.cn/img_convert/ba814fe6d128cb1be35875e1672da8ea.png)
![7a43fd29632a11d62019f5b90babcce8.png](https://img-blog.csdnimg.cn/img_convert/7a43fd29632a11d62019f5b90babcce8.png)
@WebServlet() 对应form里的action的值。
![91d54162975e007d0ac9ae03ab737a6a.png](https://img-blog.csdnimg.cn/img_convert/91d54162975e007d0ac9ae03ab737a6a.png)
![423c6b80bc75bf6125b9dbc20d194a9c.png](https://img-blog.csdnimg.cn/img_convert/423c6b80bc75bf6125b9dbc20d194a9c.png)
servlet里有两个方法,一个是doGet,一个是doPost,对应form里的method。
doGet和doPost有两个参数,request是用户请求,response是服务器响应。
![49f4d04e1d539ebe30dadf42ff3c8342.png](https://img-blog.csdnimg.cn/img_convert/49f4d04e1d539ebe30dadf42ff3c8342.png)
![304d3bee6ac6a3ce7438d36a209a83fd.png](https://img-blog.csdnimg.cn/img_convert/304d3bee6ac6a3ce7438d36a209a83fd.png)
表单提交获取值servlet从request里获取,调用request的getParameter()方法。这个request.getParameter()里的参数对应input的name属性。这俩是对应的。
![795e0e570cfab416a5c6076504a3836c.png](https://img-blog.csdnimg.cn/img_convert/795e0e570cfab416a5c6076504a3836c.png)
![1b20276b3345bf13ef277bdfc216e5f0.png](https://img-blog.csdnimg.cn/img_convert/1b20276b3345bf13ef277bdfc216e5f0.png)
到这里,servlet就可以获取前端传递的值了。
下一步建实体类bean。
User.java
![796be7716ea76a59bd2cbec5c83d368e.png](https://img-blog.csdnimg.cn/img_convert/796be7716ea76a59bd2cbec5c83d368e.png)
![94747046853feaf6ce588d0b50fe243b.png](https://img-blog.csdnimg.cn/img_convert/94747046853feaf6ce588d0b50fe243b.png)
bean类名一般和表名对应,属性和字段名对应,属性要private修饰,通过public的get和set方法取值赋值,bean体现了java三大特性之一的封装。
![eb935547b1f08adb4ea5fc535fab3524.png](https://img-blog.csdnimg.cn/img_convert/eb935547b1f08adb4ea5fc535fab3524.png)
![4f0ddd214fdb9dca6a2bfad2ee5422c7.png](https://img-blog.csdnimg.cn/img_convert/4f0ddd214fdb9dca6a2bfad2ee5422c7.png)
这是生成get、set方法的步骤,eclipse可以帮我们自动生成。
DBUtil.java
util是工具类,比如连接数据库,处理中文乱码,格式化时间日期,调用api等等,DBUtil是比较常用的,一般写好一次,以后直接复制使用,注意修改数据库地址,用户名密码。java连接mysql需要一个jar包,放到lib目录下。DBUtil里一般写个测试方法,先试试能不能连,以免后面写一堆,找不出错来。
package util;
import java.sql.*;
public class DBUtil {
// 本地数据库地址
private static final String URL = "jdbc:mysql://localhost:3306/web?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=GMT";
// 本地数据库用户名
private static final String USERNAME = "root";
// 本地数据库密码
private static final String PASSWORD = "root";
// 数据库驱动
private static final String jdbcName = "com.mysql.cj.jdbc.Driver";
/**
* 获取数据库连接
*/
public static Connection getConnection() throws Exception {
Class.forName(jdbcName);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return connection;
}
/* 测试方法 */
public static void main(String[] args) throws Exception {
Connection connection = DBUtil.getConnection();
if (connection != null) {
System.out.println("连接成功");
} else {
System.out.println("连接失败");
}
connection.close();
}
}
![425107ea1f6f149f6d8d8ef82c047785.png](https://img-blog.csdnimg.cn/img_convert/425107ea1f6f149f6d8d8ef82c047785.png)
LogDao
接下来完善Dao层。DAO(Data Access Object) 数据访问对象。dao层里面一般就写增删改查的方法,不干别的。比如现在登录,需要一个查的方法通过用户名查用户信息。
![af6d00e80b1c0f5546c8153c42374d90.png](https://img-blog.csdnimg.cn/img_convert/af6d00e80b1c0f5546c8153c42374d90.png)
下一步写sql语句,要先确保sql语句是可以用的。
select * from user where username = ?
![2c3409d5fbcea45e6e8481241a93fc80.png](https://img-blog.csdnimg.cn/img_convert/2c3409d5fbcea45e6e8481241a93fc80.png)
![608626211c4ebdcef244d701a666c6af.png](https://img-blog.csdnimg.cn/img_convert/608626211c4ebdcef244d701a666c6af.png)
![5d4191cc3cfdd56495b7d6013778ad7e.png](https://img-blog.csdnimg.cn/img_convert/5d4191cc3cfdd56495b7d6013778ad7e.png)
然后写到java里,需要注意的是参数是问号。
![2d773489a974f10bf64b3a8a693c86d3.png](https://img-blog.csdnimg.cn/img_convert/2d773489a974f10bf64b3a8a693c86d3.png)
这个方法的返回值是User对象,所以先实例化一个。
![275121a70b2f73f70f7d224350a8ee83.png](https://img-blog.csdnimg.cn/img_convert/275121a70b2f73f70f7d224350a8ee83.png)
下一步获取数据库连接。有了数据库工具类,就不用每次都写那么多连数据库的了
![1a80374df0a4b70224427b6a0105ccb3.png](https://img-blog.csdnimg.cn/img_convert/1a80374df0a4b70224427b6a0105ccb3.png)
获取数据库连接,报错一般放到横线上就会告诉你怎么改。这说调用这个方法需要抛出异常,点第一个就行。
![bea1d2f91db0d5b73c7fb7d0e69a761c.png](https://img-blog.csdnimg.cn/img_convert/bea1d2f91db0d5b73c7fb7d0e69a761c.png)
下一步是执行sql语句,这里叫预编译处理,优点是传值方便,防止XSS注入。
![209693b9fd385fb35283047753d58135.png](https://img-blog.csdnimg.cn/img_convert/209693b9fd385fb35283047753d58135.png)
![676bcb627cf76ddcea379858be19faa9.png](https://img-blog.csdnimg.cn/img_convert/676bcb627cf76ddcea379858be19faa9.png)
ResultSet这个类是获取查询结果的,实例化一个结果集对象。这个结果集有个游标,每从里面取一条记录,就往后走一个。
![134030400eb4becc7c49833943cc0d88.png](https://img-blog.csdnimg.cn/img_convert/134030400eb4becc7c49833943cc0d88.png)
![613c8a66ba34abfbabcc33a836f9f8fd.png](https://img-blog.csdnimg.cn/img_convert/613c8a66ba34abfbabcc33a836f9f8fd.png)
Resultset有个方法叫next(),调用next方法之后游标就往后走,发现有一条数据,就返回了true。
![bd5ad93161762a6534534c976bb7dff2.png](https://img-blog.csdnimg.cn/img_convert/bd5ad93161762a6534534c976bb7dff2.png)
然后从rs里获取值,rs.getInt是获取数字类型的数据,括号里的参数是字段名,按照如图所示对应。
![1071a893d1902b2bc30d2b3683af02b2.png](https://img-blog.csdnimg.cn/img_convert/1071a893d1902b2bc30d2b3683af02b2.png)
最重要的一步,数据库连接用完后一定要关闭,调用close方法!!!
![951ff3dae886b930482fcd0c9064ec5e.png](https://img-blog.csdnimg.cn/img_convert/951ff3dae886b930482fcd0c9064ec5e.png)
到这dao层的一个查询方法就写好了。
LogServlet.java
接下来完善servlet
![dd8598c35f44f8a5eaed4d68240f5c16.png](https://img-blog.csdnimg.cn/img_convert/dd8598c35f44f8a5eaed4d68240f5c16.png)
实例化一个Dao层对象。使用try catch捕获异常。
![f49420f15aff1da90d122d8d6c74767e.png](https://img-blog.csdnimg.cn/img_convert/f49420f15aff1da90d122d8d6c74767e.png)
在User类中重写toString方法,用来输出对象的值。
![63997e299db9ee0ed72934fa187cd88a.png](https://img-blog.csdnimg.cn/img_convert/63997e299db9ee0ed72934fa187cd88a.png)
三、测试效果
![9fb8a9da6408516321f0d896607ae37b.png](https://img-blog.csdnimg.cn/img_convert/9fb8a9da6408516321f0d896607ae37b.png)
![81d5ece922dada1648e759a1668bfb53.png](https://img-blog.csdnimg.cn/img_convert/81d5ece922dada1648e759a1668bfb53.png)
代码下载
GitHub:
https://github.com/wlgc1801lzh/javaweb-example
码云:
https://gitee.com/wlgc1801lzh/javaweb-example
扫码关注我们
微信号|新工技术专栏
qq群|769585700
![9343baad2a7dace1e28d0964c126e374.png](https://img-blog.csdnimg.cn/img_convert/9343baad2a7dace1e28d0964c126e374.png)