1 . 同上一篇文章, 建立工具类util用于访问数据库
包括数据库的连接以及断开
public class DBUtil {
public static Connection getConnect() throws ClassNotFoundException, SQLException {
// 1 声明mysql驱动
String driver = "com.mysql.jdbc.Driver";
// 2 声明与mysql的连接对象
Connection conn = null;
//3 声明执行sql语句
Statement stmt;
// 4 URL指向要访问的数据库名test
String url = "jdbc:mysql://localhost:3306/whlg";
// jdbc:mysql jdbc是连接协议,mysql是子协议 //localhost: 本机//3306 数据库端口号
String user = "root"; // 5 用户名
String passwords = "123456"; //6 密码
Class.forName(driver);
conn= DriverManager.getConnection(url,user,passwords);
return conn;
}
public static void close(ResultSet resultset,Statement statement,Connection conn) throws SQLException {
if(resultset!=null){
resultset.close();
}
if(statement!=null) {
statement.close();
}
if(conn!=null) {
conn.close();
}
}
}
2 . 编写实现类User,数据库中相应属性封装进去,同上一篇文章一样,此处不再展示
3 . 在Dao层对数据库进行操作,为实现登陆功能,这里我们编写根据用户名和密码一起查询,并返回相应的User类,用于实现登陆的判断。
public class UserDao {
public User findByNamePwd(String username,String password) throws SQLException, ClassNotFoundException {
User users=null;
//连接数据库
Connection conn= DBUtil.getConnect();
//写sql语句,创建statement对象
//执行sql语句,查看数据库中知否有用户名和密码对应的记录
String sql="select * from tb_user where username=? and password=?";
PreparedStatement statement=conn.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
ResultSet resultset=statement.executeQuery();
//如果存在,将User类具体化,否则为空
if(resultset.next()){
users=new User();
int id=resultset.getInt(1);
users.setId(id);
users.setUsername(username);
users.setPassword(password);
}
//关闭数据库
DBUtil.close(resultset,statement,conn);
return users;
}
public List<User> findAll() throws ClassNotFoundException, SQLException {
List<User> userlist=new ArrayList<>();
//省略
return userlist;
}
public void deleteById(int id) throws SQLException, ClassNotFoundException {
// List<User> userlist=new ArrayList<>();
Connection conn= DBUtil.getConnect();
String sql="delete from tb_user where id=?";
PreparedStatement statement=conn.prepareStatement(sql);
// statement.execute();
statement.setInt(1,id);
statement.executeUpdate();
DBUtil.close(null,statement,conn);
}
}
4 . 编写index.jsp,在里面加入一个form表单用于输入用户民和密码进行登陆,用于发出post请求
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
<style>
<!--省略具体风格代码-->
</style>
</head>
<body>
<a href="/hello">查询所有用户</a>
<div class="main">
<div class="title">
<span>密码登录</span>
</div>
<div class="title-msg">
<span>请输入登录账户和密码</span>
</div>
<form class="login-form" action="login" method="post" >
<!--输入框-->
<div class="input-content">
<!--autoFocus-->
<div>
<input type="text" autocomplete="off"
placeholder="用户名" name="username"/>
</div>
<div style="margin-top: 16px">
<input type="password"
autocomplete="off" placeholder="登录密码" name="password" maxlength="32"/>
</div>
</div>
<!--登入按钮-->
<div style="text-align: center">
<button type="submit" class="enter-btn" >登录</button>
</div>
</form>
</div>
</body>
</html>
5 . 同时写与其对应的servlet,LoginServlet接收post请求并接受用户名和密码,进行匹配后将结果返回
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收前端传来的用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//调用UserDao类数据库处理
UserDao userDao=new UserDao();
User user=null;
try {
user=userDao.findByNamePwd(username,password);
//如果存在这样的用户名和密码,跳转到success.jsp界面
if(user!=null){
System.out.println("login success");
resp.sendRedirect("success.jsp");
}
//否则还在原来界面
else{
System.out.println("login fail");
resp.sendRedirect("index.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
6 . 在web.xml中编写相应的映射,servlet和servlet-mapping进行对应,通过servlet-name作为纽带,将servlet-class和url-pattern构成联系,从而使URL映射到类servlet-class所指定的类中
相应的servlet对登陆逻辑业务进行处理,并传回相应界面
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.zr.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
最终登陆界面如下:

登陆成功显示success.jsp页面:

本文详细介绍了使用Java Web技术实现用户登陆系统的全过程,从数据库连接工具类的搭建,到User类的设计,再到DAO层的数据库操作,最后实现index.jsp表单登陆及Servlet处理流程。
3751

被折叠的 条评论
为什么被折叠?



