登录注册及免密的实现
程序员文章站
2024-03-13 17:22:09
...
在实现以下功能前,我们得了解JDBC的封装以及Cookie如何使用。
对于以上不清楚的请移步我的博文JDBC工具类的封装和Cookie基础使用
用户登录
1、创建用户信息表users
列名 | 数据类型 | 允许Null值 |
---|---|---|
userId(主键) | int | 否 |
username | varchar(50) | 否 |
password | varchar(50) | 否 |
sex | char(1) | 否 |
varchar (50) | 否 |
注意:userId为auto_ increment,自增序列
关于如何在SqlServer中设置自增?
首先我们把主键的数据类型设为int,然后再下方属性中的标识规范中【(是表识)】设置为【是】
2、login.jsp(登录页面)
<form action="/myWeb/login" method="post">
用户名:<input type="text" name="username" value="${cookie.username.value}" /><br>
密码:<input type="password" name="password" value="${cookie.password.value}"/><br>
<input type="submit" value="登录">
<a href="user_Add.html">注册</a>
</form>
3、user_Add.html(注册页面)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户注册页面</title>
</head>
<body>
<center>
<form action="/myWeb/user/add" method="get">
<table border="2">
<tr>
<td>用户姓名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>用户密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>用户性别</td>
<td>
<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td>用户邮箱</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td><input type="submit" value="用户注册"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
4、Users(实体类)
package com.university.entity;
public class Users {
private Integer userId;
private String username;
private String password;
private String sex;
private String email;
public Users() {
}
public Users(Integer userId, String username, String password, String sex, String email) {
this.userId = userId;
this.username = username;
this.password = password;
this.sex = sex;
this.email = email;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
5、LoginServlet(登录逻辑处理)
package com.university.controller;
import com.university.dao.UserDao;
import com.university.entity.Users;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao dao = new UserDao();
Users user = null;
//1、调用请求对象对请求体使用utf-8字符集进行重新编辑
request.setCharacterEncoding("UTF-8");
//2、调用请求对象读取请求体参数信息
String username = request.getParameter("username");
String password = request.getParameter("password");
//3、调用Dao将查询验证信息推送到数据库服务器上
user = dao.login(username,password);
//4、调用响应对象,根据验证结果将不同资源文件地址写入到响应头,交给浏览器
response.setContentType("text/html;charset=UTF-8");
if (user != null){
//使用Cookie实现免密登录
Cookie cookie1 = new Cookie("username",username);
Cookie cookie2 = new Cookie("password",password);
cookie1.setMaxAge(60 * 60 * 24 * 5);
cookie2.setMaxAge(60 * 60 * 24 * 5);
response.addCookie(cookie1);
response.addCookie(cookie2);
response.sendRedirect(request.getContextPath() + "/index.html");
} else {
response.getWriter().println("<font style='color:red;font-size:40'>用户名或密码错误</font>");
}
}
}
6、UserAddServlet(注册逻辑处理)
package com.university.controller;
import com.university.dao.UserDao;
import com.university.entity.Users;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class UserAddServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao dao = new UserDao();
Users user = null;
int result = 0;
PrintWriter out = null;
//1、【调用请求对象】读取【请求头】参数信息,得到用户的信息信息
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
//2、【调用UserDao】将用户信息填充到INSERT命令并借助JDBC规范发送到数据库服务器
user = new Users(null,username,password,sex,email);
result = dao.add(user);
//3、【调用响应对象】将【处理结果】以二进制形式写入到响应体
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
if (result == 1){
response.sendRedirect(request.getContextPath() + "/login.jsp");
} else {
out.println("<font style='color:red;font-size:40'>用户信息注册失败</font>");
}
}
//Tomcat负责销毁【请求对象】和【响应对象】
//Tomcat负责将Http响应协议包推送到发起请求的浏览器上
//浏览器根据响应头content-type指定编译器对响应体二进制内容编辑
//浏览器将编辑后结果在窗口中展示给用户【结束】
}
7、web.xml(Servlet配置)
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.university.controller.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>UserAddServlet</servlet-name>
<servlet-class>com.university.controller.UserAddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserAddServlet</servlet-name>
<url-pattern>/user/add</url-pattern>
</servlet-mapping>
8、UserDao(数据库操作)
package com.university.dao;
import com.university.entity.Users;
import com.university.util.JdbcUtil;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
private JdbcUtil util = new JdbcUtil();
//用户登录
public Users login(String username,String password){
String sql = "select * from users where username=? and password=?";
Users user = null;
ResultSet rs = null;
try {
PreparedStatement ps = util.createStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
while (rs.next()){
user = new Users();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
util.close(rs);
}
return user;
}
//用户注册
public int add(Users user){
String sql = "insert into users(username,password,sex,email) values(?,?,?,?)";
int result = 0;
try {
PreparedStatement ps = util.createStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setString(3,user.getSex());
ps.setString(4,user.getEmail());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
util.close();
}
return result;
}
}
9、测试
(1)查看自定义的页面
(2)注册
(3)点击【用户注册】按钮后跳转到【登录页面】,此时输入用户名和密码登录
(4)输入错误密码或用户名
(5)正式登录
(6)此时退出浏览器,重新到【登录页面】,此时发现用户名与密码已经显示在页面上了,不在需要我们手动输入
上一篇: Docker学习笔记杂记