本文最后更新于 2024-09-12,文章内容可能已经过时。

1+X职业技能等级证书

数据库

xwebdb

数据表user

id int 自增 主键 非空

username varchar(20)

password varchar(20)

新建项目xwebdb

包名

com.neu.bean

com.neu.dao

com.neu.service

com.neu.servlet

com.neu.util

一、 在bean下建User.java

package com.neu.bean;
​
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(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }
    public User() {
        super();
    }
}

二、 在util下新建DButils 工具类(连接数据库)

import java.sql.*;
public class DButils {//连接数据库
    private static final String DRIVER="com.mysql.jdbc.Driver";
    private static final String URL="jdbc:mysql://localhost/xwebdb";
    private static final String USERNAME="root";
    private static final String PASSWORD="root";
    static {
        try {
            Class.forName(DRIVER);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

三、 在Dao下创建UserDao (用户登录,注册,增,删,改,查) 重点

package com.neu.dao;
import java.sql.*;
​
import com.neu.bean.User;
import com.neu.util.DButils;
public class userDao {
    private Connection conn=null;
    private PreparedStatement pstat=null;
    private ResultSet rs=null;
    public User findByNameAndPassword(String username,String password) {
        //在此处写代码
        try {
            conn=DButils.getConnection();
            String sql="select * from user where username=? and password=?";
            pstat=conn.prepareStatement(sql);
            pstat.setString(1, username);
            pstat.setString(2, password);
            rs=pstat.executeQuery();
            User user=new User();
            while(rs.next()) {
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
            return user;
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
        return null;
    }
}

四、创建 userService

package com.neu.service;
​
import com.neu.bean.User;
import com.neu.dao.userDao;
​
public class userService {//服务,调用dao中的功能
       private userDao dao=new userDao();
       public User findByNameAndPassword(String username,String password) {
            return dao.findByNameAndPassword(username, password) ; 
       }
}

五、 创建login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="userLoginServlet">
     <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="提交"> </td>
        </tr>
     </table>
</form>
</body>
</html>

六、 创建servlet

package com.neu.servlet;
​
import java.io.IOException;
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 com.neu.bean.User;
import com.neu.service.userService;
​
@WebServlet("/userLoginServlet")
public class userLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;   
​
    public userLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
​
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //在此处写代码
        //解决乱码
        request.setCharacterEncoding("UTF-8");
        //获取用户名和密码
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //创建userService;
        userService service=new userService();
        User user=service.findByNameAndPassword(username, password);
        if(user!=null) {
            //如果找到数据库中用户名和密码,跳转到index.jsp
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
        else {
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        
    }
​
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        doGet(request, response);
    }
}