User类代码:
package a;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
controlserviet 类代码:
package b;
import a.User;
import c.dao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/controlservite")
public class controlserviet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String type = request.getParameter("type");
// System.out.println(username+password+type);
dao dao =new dao();
User user = dao.login(username,password,type);
HttpSession session = request.getSession();
if(type.equals("登录")){
if(user == null){
session.setAttribute("err","<h1>用户不存在</h1>");
response.sendRedirect("err.jsp");
}else {
session.setAttribute("success","<h1>登录成功</h1>");
response.sendRedirect("success.jsp");}
}else {
if(user == null){
session.setAttribute("err","<h1>注册失败</h1>");
response.sendRedirect("err.jsp");
}else {
session.setAttribute("success","<h1>注册成功</h1><h3>账号:"+username+"</h3><h3>密码:"+password+"</h3>");
response.sendRedirect("success.jsp");
}}}
}
Dao类的代码:
package c;
import a.User;
import com.alibaba.druid.util.JdbcUtils;
import utli.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class dao {//在dao类中查询数据库
public User login(String username,String password,String type){
//创建user变量,用来存储返回的结果
User user = null;
//创建pst变量,用来存储sql预编译的结果,通过这个预编译来给sql语句中的问号位置赋值
PreparedStatement pst = null;
//创建rs变量,用来存储结果集
ResultSet rs = null;
//创建数据库连接对象,jdbcUtil是连接池类,老师发下来的
Connection connection = JdbcUtil.getConnection();
//写sql语句
//这里面的两个问号是占位符,可以在预编译的时候设置占位符内容
String sql1 = "select * from myuser where username = ? and passwd = ?";
String sql2 = "insert into myuser(username,passwd) values(?,?)";
try {
if(type.equals("登录")){
pst = connection.prepareStatement(sql1);
//设置第一个问号的内容,username和password变量是调用这个类的时候传进来的参数
pst.setString(1,username);
//设置第二个问号的内容
pst.setString(2,password);
//设置好了sql语句之后开始执行sql语句
rs = pst.executeQuery();
// System.out.println("rs:"+rs);
//遍历结果集
while (rs.next()){
// 每次遍历就将获取的具体参数传递给User类,然后用上面创建的user变量接收new出来的User对象
user = new User(rs.getInt("id"),rs.getString("username"),rs.getString("passwd"));
}
}else {
pst = connection.prepareStatement(sql2);
//设置第一个问号的内容,username和password变量是调用这个类的时候传进来的参数
pst.setString(1,username);
//设置第二个问号的内容
pst.setString(2,password);
//设置好了sql语句之后开始执行sql语句
int x = pst.executeUpdate();
pst = connection.prepareStatement(sql1);
//设置第一个问号的内容,username和password变量是调用这个类的时候传进来的参数
pst.setString(1,username);
//设置第二个问号的内容
pst.setString(2,password);
//设置好了sql语句之后开始执行sql语句
rs = pst.executeQuery();
// System.out.println("rs:"+rs);
//遍历结果集
while (rs.next()){
// 每次遍历就将获取的具体参数传递给User类,然后用上面创建的user变量接收new出来的User对象
user = new User(rs.getInt("id"),rs.getString("username"),rs.getString("passwd"));
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JdbcUtil.closeConnection(connection,pst,rs);
}
//在这里返回user变量,调用这个类时如果返回null就说明没有查询到这个人的信息,如果不是null就说明有这个人的信息
return user;
}
}
JdbcUtil (jdbc工具类)代码:
package utli;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* jdbc工具类
*/
public class JdbcUtil {
//数据源对象(连接池)
private static DataSource dataSource;
//加载数据库连接信息到DataSource
static {
try {
//创建Properties属性文件
Properties properties=new Properties();
//将Properties属性文件转换为字符输入流
InputStream inputStream=JdbcUtil.class.getResourceAsStream("/db.properties");
//加载流
properties.load(inputStream);
//创建连接池对象
dataSource= DruidDataSourceFactory.createDataSource(properties);
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从数据库中取出一个connection对象
* @return
*/
public static Connection getConnection(){
Connection connection=null;
try {
connection=dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 关闭连接(Connection连接对象必须在最后关闭)
* @param connection Connection连接对象
* @param pstm 编译执行对象
* @param result 结果集
*/
public static void closeConnection(Connection connection,PreparedStatement pstm, ResultSet result){
try {
if(result != null){
result.close();
}
if(pstm != null){
pstm.close();
}
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Err.jsp文件代码:
<%--
Created by IntelliJ IDEA.
User: juta
Date: 2023/12/10
Time: 10:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${sessionScope.err}
</body>
</html>
Login.jsp文件:
<%--
Created by IntelliJ IDEA.
User: juta
Date: 2023/12/9
Time: 22:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="controlservite" method="post">
用户名: <input type="text" name="username"/><br>
密码: <input type="text" name="password"/><br>
<input type="submit" value="登录" name="type" /><br>
</form>
</body>
</html>
Register.jsp文件代码:
<%--
Created by IntelliJ IDEA.
User: juta
Date: 2023/12/9
Time: 22:10
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>--%>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="controlservite" method="post">
用户名: <input type="text" name="username"/><br>
密码: <input type="text" name="password" /><br>
<input type="submit" value="注册" name="type"/><br>
</form>
</body>
</html>
Success.jsp文件内容:
<%--
Created by IntelliJ IDEA.
User: juta
Date: 2023/12/10
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Title</title>
</head>
<body>
${sessionScope.success}
</body>
</html>