需求:
使用Servlet+HTML+Mysql实现用户注册/登录/查询用户信息功能
创建坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
引入JBDC工具类
public class BaseDao {
// 数据库的连接url
private String url = "jdbc:mysql://127.0.0.1:3306/crm-idea?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
// 用户名
private String user = "root";
// 密码
private String password = "123456";
// 驱动串
private String className = "com.mysql.cj.jdbc.Driver";
private Connection connection;
private PreparedStatement pst;
private ResultSet rs;
// 1、建立连接,获取Connection对象
public Connection getConnection() {
if(this.connection == null) {
try {
Class.forName(className);
connection = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
return connection;
}
// 2、获取PreparedStatement对象
public PreparedStatement getPreparedStatement(String sql) {
if(this.pst == null) {
try {
this.pst = getConnection().prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
return this.pst;
}
// 3、执行sql--executeUpdate
public int executeUpdate(String sql) {
int count = 0;
try {
count = getPreparedStatement(sql).executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll();
}
return count;
}
// 4、执行sql--executeQuery
public ResultSet executeQuery(String sql) {
try {
rs = getPreparedStatement(sql).executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// 5、释放资源
public void closeAll() {
try {
if(this.rs != null) {
this.rs.close();
this.rs = null;
}
if(this.pst != null) {
this.pst.close();
this.pst = null;
}
if(this.connection != null) {
this.connection.close();
this.connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
编辑java文件-注册
@WebServlet("/enroll")
public class enroll extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题 .setCharacterEncoding .setContentType
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf8");
//接受用户在html输入的用户名,密码和确认密码
String name = req.getParameter("name");
String pwd = req.getParameter("pwd");
String passwd = req.getParameter("passwd");
//打印输出流,向网页输出内容
PrintWriter writer = resp.getWriter();
if (pwd.equals(passwd)){
//dao对象实例化,实现与数据库链接
BaseDao dao = new BaseDao();
//编辑sql语句
String sql = "insert into t_user(name,password) values('"+name+"','"+pwd+"')";
//调用新增方法返回结果
int count = dao.executeUpdate(sql);
//结果判断
if (count>0){
writer.println("注册成功");
}else{
writer.println("参数错误/网络异常,注册失败");
}
}else{
writer.println("<script>alert('The passwords entered twice are inconsistent')</script>");
}
writer.close();
}
}
编辑html文件-注册
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="login">
用户名 <input type="text" name="name"> <br/>
密码 <input type="password" name="pwd"> <br/>
确认密码 <input type="password" name="passwd"> <br/>
<button type="submit">登录</button>
<button type="reset">重置</button>
</form>
</body>
</html>
编辑java文件-登录
@WebServlet("/login")
public class login extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题 .setCharacterEncoding .setContentType
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf8");
String name = req.getParameter("name");
String pwd = req.getParameter("pwd");
String passwd = req.getParameter("passwd");
PrintWriter writer = resp.getWriter();
if (pwd.equals(passwd)){
BaseDao dao = new BaseDao();
String sql = "select * from t_user where name='"+name+"' and password='"+pwd+"'";
ResultSet rs = dao.executeQuery(sql);
try {
if(rs.next()){
writer.println("登陆成功");
}else{
writer.println("用户名或密码错误,登录失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
//关流
dao.closeAll();
}else {
writer.println("<script>alert('The passwords entered twice are inconsistent')</script>");
}
writer.close();
}
}
编辑html文件-登录
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form action="enroll">
用户名 <input type="text" name="name"> <br/>
密码 <input type="password" name="pwd"> <br/>
确认密码 <input type="password" name="passwd"> <br/>
<button type="submit">注册</button>
<button type="reset">重置</button>
</form>
</body>
</html>
编辑java文件-查询所有
@WebServlet("/selectAll")
public class selectAll extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf8");
BaseDao dao = new BaseDao();
String sql = "select * from t_user ";
ResultSet rs = dao.executeQuery(sql);
PrintWriter writer = resp.getWriter();
try {
while (rs.next()){
//获取数据库中的账号,密码
String name = rs.getString("name");
String password = rs.getString("password");
//将获取的账号密码打印至页面
writer.println(name);
writer.println(password);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
dao.closeAll();
}
}
编辑html文件-查询所有
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>查询所有</h1>
<form action="selectAll">
<button type="submit">点击查询</button>
</form>
</body>
</html>