@2024/12/04 08:44
一、项目结构图
二、User 类
package Bean;
public class User {
private int id;
private String username;
private String password;
private String sex;
private String email;
private String birthday;
public User() {
}
public User(int id, String username, String password, String sex, String email, String birthday) {
this.id = id;
this.username = username;
this.password = password;
this.sex = sex;
this.email = email;
this.birthday = birthday;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
三、UserDao 类
package Dao;
import Util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
ResultSet rs;
PreparedStatement pstm;
//登录
public boolean Login(String name, String pwd) {
//调用 jdbc 连接
Connection connection = JDBCUtil.getconnection();
String sql = "select * from tb_user where username= ? and password = ?";
try {
//执行sql语句
pstm=connection.prepareStatement(sql);
//赋值
pstm.setString(1, name);
pstm.setString(2, pwd);
//执行
rs = pstm.executeQuery();
if(rs.next()){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
四、UserService
package Service;
import Dao.UserDao;
public class UserService {
//实例化 UserDao
private UserDao dao = new UserDao();
//登录方法,UserService 调用 UserDao 中的 登录方法
public boolean Login(String name, String pwd) {
return dao.Login(name, pwd);
}
}
五、UserServlet
1、UserServlet 类解析
2、UserServlet 类
package Servlet;
import Service.UserService;
import jakarta.servlet.http.*;
import java.io.IOException;
public class UserServlet extends HttpServlet {
//实例化 UserService
private UserService userService = new UserService();
public UserServlet(){
super();
}
public void destroy(){
super.destroy();
}
public void doGet(HttpServletRequest request,HttpServletResponse response){
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
//页面登录
String name = request.getParameter("username");
String password = request.getParameter("pwd");
if(userService.Login(name,password)){
response.sendRedirect("/UserManager_war_exploded/success.jsp");
}else {
response.sendRedirect("/UserManager_war_exploded/fail.jsp");
}
}
public void init() {
}
}
六、Util
package Util;
import java.sql.*;
public class JDBCUtil {
//加载驱动
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getconnection() {
String url="jdbc:mysql://localhost:3306/jdbc_demo?userUnicode=true&characterEncoding=utf8";
String user="root";
String password="abc123";
Connection conn=null;
try {
conn= DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//释放资源
public static void release(ResultSet rs,PreparedStatement pstm,Connection conn){
try{
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if(conn!=null){
conn.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
七、web.xml 文件
1、web.xml 配置图解析
2、web 配置文件
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>Servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/servlet/UserServlet</url-pattern>
</servlet-mapping>
八、页面
1、index.jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title><span data-type="inline-math" data-subtype="math" data-content="Title" contenteditable="false" class="render-node"></span></title>
</head>
<body>
<form name="form" id="form" method="post" action="/UserManager_war_exploded/servlet/UserServlet">
<p>用户名:<input type="text" name="username"></p>
<p>密 码:<input type="password" name="pwd"></p>
<p>
<input type="submit" value="提交">
<input type="reset" value="重置">
</p>
</form>
</body>
</html>
2、success.jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
3、fail.jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h>登录失败</h>
</body>
</html>
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于