校园论坛(Java)—— 登录注册和用户信息模块
文章目录
1、写在前面
- Windows版本:Windows10
- JDK版本:Java8
- MySQL版本:MySQL5.7
- Tomcat版本:Tomcat9.0
- IDE:IntelliJ IDEA Ultimate2020.2.3
2、系统结构设计
2.1 各个页面之间的调用关系
登录管理模块
login.jsp
:登录界面check_login()函数
:用于判断用户名和密码是否正确home.jsp
:登录成功进入主页界面
注册模块
register.jsp
:该界面用于新用户注册RegisterRepeatUserIdAck.jsp
:提示当前注册的ID已经被注册过number.jsp
:注册时验证码的生成功能login.jsp
:新用户注册完成后跳转到登录界面
用户信息模块
individualinfo.jsp
:显示个人信息的页面updateinfo.jsp
:修改个人信息界面check_login()函数
:修改功能会进行验证用户名和密码是否匹配
2.2. 登录注册模块各层的设计
- Entity层
实体层声明普通帖子的
user_id、user_name、user_password、user_sex、user_face、user_phone、user_email、user_from、isAdmin
等变量以及对应的set、get方法
/**
* TODO 用户实体类
*/
public class UserEntity {
// private String user_id;
private int user_id;
private String user_name;
private String user_password;
private String user_sex;
private String user_face;
private String user_phone;
private String user_email;
private String user_from;
private int isAdmin;
// 关联回复信息,一对多的关联
private List<ReplyEntity> replyList = new ArrayList<ReplyEntity>();
// 和帖子进行关联,一对多
private List<ForumEntity> forumList = new ArrayList<ForumEntity>();
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public String getUser_sex() {
return user_sex;
}
public void setUser_sex(String user_sex) {
this.user_sex = user_sex;
}
public String getUser_face() {
return user_face;
}
public void setUser_face(String user_face) {
this.user_face = user_face;
}
public String getUser_phone() {
return user_phone;
}
public void setUser_phone(String user_phone) {
this.user_phone = user_phone;
}
public String getUser_email() {
return user_email;
}
public void setUser_email(String user_email) {
this.user_email = user_email;
}
public String getUser_from() {
return user_from;
}
public void setUser_from(String user_from) {
this.user_from = user_from;
}
public int getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(int isAdmin) {
this.isAdmin = isAdmin;
}
public List<ReplyEntity> getReplyList() {
return replyList;
}
public void setReplyList(List<ReplyEntity> replyList) {
this.replyList = replyList;
}
public List<ForumEntity> getForumList() {
return forumList;
}
public void setForumList(List<ForumEntity> forumList) {
this.forumList = forumList;
}
}
- Dao层
Dao层分别实现一个
interface
接口类以及一个接口方法
实现类
接口类:
public interface UserDaoInf {
// TODO 登录时作用户的验证查询
public UserEntity findUser(String userId, String passwd);
// TODO 根据id查询用户
public UserEntity findUserById(int id);
// TODO 添加用户
public int addUser(UserEntity user);
// TODO 删除用户
public void deleteUser(int uid);
// TODO 修改用户
public void updateUser(UserEntity user);
// TODO 查询所有用户信息
public List<UserEntity> findUserlist();
}
接口实现方法
public class UserDaoImpl implements UserDaoInf{
public UserEntity findUser(String userId, String passwd) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where user_id=? and user_password=?";
UserEntity user = null;
try{
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, userId);
ps.setString(2, passwd);
rs = ps.executeQuery();
if(rs.next()){
user = new UserEntity();
user.setUser_id(rs.getInt(1));
user.setUser_name(rs.getString(2));
user.setUser_password(rs.getString(3));
user.setUser_sex(rs.getString(4));
user.setUser_face(rs.getString(5));
user.setUser_phone(rs.getString(6));
user.setUser_email(rs.getString(7));
user.setUser_from(rs.getString(8));
user.setIsAdmin(rs.getInt(9));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return user;
}
public int addUser(UserEntity user) {
Connection conn = null;
PreparedStatement ps = null;
String isRepeatUserId = "";
int i = 0;
String repeatIDSql = "SELECT \"true\" FROM user_info where user_id = " + user.getUser_id();
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(repeatIDSql);
ResultSet resultSet = ps.executeQuery();
if (resultSet.next()) {
isRepeatUserId = resultSet.getString(1);
}
System.out.println("查询完成!");
if ("true".equals(isRepeatUserId)) {
System.out.println(isRepeatUserId);
return 0;
} else {
String sql = "insert into user_info(user_id,user_name,user_password,user_sex,user_face,user_phone,user_email,user_from) values(?,?,?,?,?,?,?,?)";
System.out.println(user.getUser_id());
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, user.getUser_id());
ps.setString(2, user.getUser_name());
ps.setString(3, user.getUser_password());
ps.setString(4, user.getUser_sex());
ps.setString(5, user.getUser_face());
ps.setString(6, user.getUser_phone());
ps.setString(7, user.getUser_email());
ps.setString(8, user.getUser_from());
i = ps.executeUpdate();
System.out.println("插入数据库成功");
} catch (Exception e){
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
}
} catch (Exception e){
e.printStackTrace();
} finally {
DBUtil.close(null, ps, conn);
}
return i;
}
public void deleteUser(int uid){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// TODO SQL:
String sql = "delete from user_info where user_id = ?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, uid);
ps.executeUpdate();
System.out.println("删除用户成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
}
public void updateUser(UserEntity user){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql= "update user_info set user_name=?,user_password=?,user_sex=?,user_face=?,user_phone=?,user_email=?,user_from=? where user_id=?";
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUser_name());
ps.setString(2, user.getUser_password());
ps.setString(3, user.getUser_sex());
ps.setString(4, user.getUser_face());
ps.setString(5, user.getUser_phone());
ps.setString(6, user.getUser_email());
ps.setString(7, user.getUser_from());
ps.setInt(8, user.getUser_id());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
}
public UserEntity findUserById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_info where user_id=?";
UserEntity user = null;
try{
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
user = new UserEntity();
user.setUser_id(rs.getInt(1));
user.setUser_name(rs.getString(2));
user.setUser_password(rs.getString(3));
user.setUser_sex(rs.getString(4));
user.setUser_face(rs.getString(5));
user.setUser_phone(rs.getString(6));
user.setUser_email(rs.getString(7));
user.setUser_from(rs.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return user;
}
public List<UserEntity> findUserlist(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<UserEntity> userlist = new ArrayList<UserEntity>();
String sql="select * from user_info";
try {
conn=DBUtil.getConnection();
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
UserEntity user = new UserEntity();
user.setUser_id(rs.getInt(1));
user.setUser_name(rs.getString(2));
user.setUser_password(rs.getString(3));
user.setUser_sex(rs.getString(4));
user.setUser_face(rs.getString(5));
user.setUser_phone(rs.getString(6));
user.setUser_email(rs.getString(7));
user.setUser_from(rs.getString(8));
user.setIsAdmin(rs.getInt(9));
userlist.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, ps, conn);
}
return userlist;
}
}
- service层
实现servlet层调用的方法,对数据库进行增删改查操作。
- Servlet层
在该层,通过jsp页面传递的参数,调用相应的方法进行操作。
- filter层
拦截过滤层,对注册用户时,验证码的过滤操作
public class EncodingFilter implements Filter {
private String encode;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (encode != null) {
request.setCharacterEncoding(encode);
response.setCharacterEncoding(encode);
}
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
encode = config.getInitParameter("encode");
}
}
3、登录注册模块设计
3.1 用户注册功能
进入登录页面后,对于第一次登录的用户来说,首先需要注册,单击「新用户注册」按钮即可进入注册界面,注册完成后返回登录界面。然后,在对应的地方分别输入用户名和密码,点击「登录」按钮,系统即将用户名和密码发送到网络服务器上,与保存在服务器数据库中的信息进行核对。若核对正确,则进入帖子浏览的界面,若不正确,会有提示账号或者密码出错,随即重新返回登录界面。
注册界面如下所示:
register.jsp
页面实现注册界面,同时判断用户ID是否已被注册过了,若被注册过,则RegisterRepeatUserIdAck.jsp
会提示已注册。注册的最后一步需要通过验证码
验证。信息填写完整并通过验证码验证后,由「注册」按钮实现用户增加的逻辑操作。
用户ID判空功能
学号判重
学号检验
联系电话检验
邮箱格式检验
验证码检验
3.2 用户登录功能
登录界面的制作
先考虑登录主界面的制作,再通过JavaScript标记语言判断输入的用户名和密码是否为空;
登录界面如下所示:
登录功能的实现
通过login.jsp
界面传递参数user_id
和user_password
给 LoginServlet.java
,然后查询数据库,判断用户名和密码是否正确,对于密码在传输的过程中会使用MD5
进行加密
(hex_md5(user_password))。若判断正确则为用户建立一个Session
,并进入了Fosu校园论坛主界面。
4、用户个人信息
4.1 查看个人信息
在系统右上角选择
个人信息
一栏,即可查看个人信息。
如下图所示:
4.2 修改个人信息
选择「修改资料」按钮即可修改用户个人信息,包括用户名、密码、性别、联系电话、电子邮箱、地址、头像的更改。
如下图所示:
7、项目代码
本文来自博客园,作者:{WHYBIGDATA},转载请注明原文链接:https://www.cnblogs.com/shadowlim/p/17051741.html