写一个java用户注册登录系统
时间: 2023-08-16 17:09:45 浏览: 82
好的,我可以给您提供一个简单的 Java 用户注册登录系统的实现代码,代码如下:
User.java
```
public class User {
private String username;
private String password;
private String email;
private String phone;
public User(String username, String password, String email, String phone) {
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
}
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
UserDAO.java
```
public class UserDAO {
private List<User> userList = new ArrayList<>();
public void addUser(User user) {
userList.add(user);
}
public User getUserByUsername(String username) {
for (User user : userList) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
public boolean isUsernameExist(String username) {
for (User user : userList) {
if (user.getUsername().equals(username)) {
return true;
}
}
return false;
}
public boolean isEmailExist(String email) {
for (User user : userList) {
if (user.getEmail().equals(email)) {
return true;
}
}
return false;
}
public boolean isPhoneExist(String phone) {
for (User user : userList) {
if (user.getPhone().equals(phone)) {
return true;
}
}
return false;
}
}
```
RegisterServlet.java
```
public class RegisterServlet extends HttpServlet {
private UserDAO userDAO = new UserDAO();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
if (userDAO.isUsernameExist(username)) {
request.setAttribute("message", "用户名已存在");
request.getRequestDispatcher("register.jsp").forward(request, response);
} else if (userDAO.isEmailExist(email)) {
request.setAttribute("message", "邮箱已存在");
request.getRequestDispatcher("register.jsp").forward(request, response);
} else if (userDAO.isPhoneExist(phone)) {
request.setAttribute("message", "手机号已存在");
request.getRequestDispatcher("register.jsp").forward(request, response);
} else {
User user = new User(username, password, email, phone);
userDAO.addUser(user);
request.getSession().setAttribute("user", user);
response.sendRedirect("home.jsp");
}
}
}
```
LoginServlet.java
```
public class LoginServlet extends HttpServlet {
private UserDAO userDAO = new UserDAO();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.getUserByUsername(username);
if (user == null) {
request.setAttribute("message", "用户名不存在");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if (!user.getPassword().equals(password)) {
request.setAttribute("message", "密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else {
request.getSession().setAttribute("user", user);
response.sendRedirect("home.jsp");
}
}
}
```
这些代码实现了用户注册、登录的基本功能,包括用户信息的存储、用户信息的查询和验证等等。当用户注册成功或登录成功时,将用户信息存储到 session 中,以便后续的操作。当用户注册信息或登录信息验证不通过时,给用户相应的提示信息,让其重新填写。
阅读全文