mvcz注册实例mysql_MVC(JSP+JavaBean+Servlet)入口实例:用户注册验证,要有User、UserDao、registerservlet、register.jsp
时间: 2024-02-04 21:03:15 浏览: 100
以下是一个简单的基于 MVC 模式的用户注册验证实例,使用 JSP、JavaBean 和 Servlet,以及 MySQL 数据库:
首先是 User 类,用于存储用户信息和提供 get/set 方法:
```java
public class User {
private String username;
private String password;
private String email;
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;
}
}
```
接下来是 UserDao 类,用于处理与数据库的交互:
```java
public class UserDao {
private Connection conn = null;
private PreparedStatement psmt = null;
private ResultSet rs = null;
// 连接数据库
public UserDao() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
} catch (Exception e) {
e.printStackTrace();
}
}
// 检查用户名是否已经存在
public boolean checkUser(String username) {
try {
psmt = conn.prepareStatement("select * from user where username=?");
psmt.setString(1, username);
rs = psmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return false;
}
// 添加用户
public boolean addUser(User user) {
try {
psmt = conn.prepareStatement("insert into user(username,password,email) values(?,?,?)");
psmt.setString(1, user.getUsername());
psmt.setString(2, user.getPassword());
psmt.setString(3, user.getEmail());
int count = psmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
return false;
}
// 关闭连接
private void close() {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
然后是 RegisterServlet 类,用于处理用户提交的表单数据和验证:
```java
public class RegisterServlet extends HttpServlet {
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");
UserDao userDao = new UserDao();
if (userDao.checkUser(username)) {
// 用户名已经存在
response.sendRedirect("register.jsp?error=1");
} else {
// 添加用户
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
if (userDao.addUser(user)) {
response.sendRedirect("register.jsp?error=0");
} else {
response.sendRedirect("register.jsp?error=2");
}
}
}
}
```
最后是 register.jsp,用于显示表单和处理服务器返回的消息:
```jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="UserDao" %>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<h1>用户注册</h1>
<% if (request.getParameter("error") != null) { %>
<%
int error = Integer.parseInt(request.getParameter("error"));
switch (error) {
case 0:
out.println("<p>注册成功,请登录!</p>");
break;
case 1:
out.println("<p>用户名已经存在,请重新输入!</p>");
break;
case 2:
out.println("<p>注册失败,请稍后重试!</p>");
break;
}
%>
<% } %>
<form action="register" method="post">
<p>用户名:<input type="text" name="username"></p>
<p>密码:<input type="password" name="password"></p>
<p>邮箱:<input type="email" name="email"></p>
<p><input type="submit" value="注册"></p>
</form>
</body>
</html>
```
以上就是一个简单的基于 MVC 模式的用户注册验证实例,使用 JSP、JavaBean 和 Servlet,以及 MySQL 数据库。注意,在实际开发中,还应该进行更多的输入验证和错误处理,以及加强数据库安全性。
阅读全文