Jsp a)注册页面中需要输入用户名、密码、确认密码以及身份的选择和验证码的输入。 b) 注册页面中的每个输入均需判断是否为空,为空进行提示。 C) 注册的用户名必须是从未有人注册的,如已有,请提示:“用户名已存在!“ e)密码和确认密码必须保持一致,否则,请提示:“两次输入的密码不一致!”。 f) 验证码为4位随机数字,当输入不正确时,请提示:“验证码输入错误!” g) 以上条件均满足,请跳转至登录页面。 h) 数据库请自行设计。 jspmysql代码
时间: 2024-02-11 09:04:03 浏览: 140
以下是一个简单的JSP+MySQL实现注册页面的示例代码:
1. 创建一个名为register.jsp的JSP页面,包含一个注册表单:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<h1>注册页面</h1>
<form action="register.do" method="post">
<p>
用户名:<input type="text" name="username">
</p>
<p>
密码:<input type="password" name="password">
</p>
<p>
确认密码:<input type="password" name="confirm_password">
</p>
<p>
身份:
<select name="role">
<option value="teacher">教师</option>
<option value="student">学生</option>
<option value="admin">管理员</option>
</select>
</p>
<p>
验证码:<input type="text" name="verification">
<img src="verification.do" onclick="this.src='verification.do?time='+new Date().getTime();" />
</p>
<p>
<input type="submit" value="注册">
</p>
</form>
</body>
</html>
```
2. 创建一个名为register.do的Servlet,处理注册请求:
```
package com.example;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String SELECT_SQL = "SELECT COUNT(*) FROM users WHERE username=?";
private static final String INSERT_SQL = "INSERT INTO users (username, password, role) VALUES (?, ?, ?)";
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirm_password = request.getParameter("confirm_password");
String role = request.getParameter("role");
String verification = request.getParameter("verification");
if (username == null || username.trim().equals("")) {
out.println("<script>alert('请输入用户名!');history.back();</script>");
return;
}
if (password == null || password.trim().equals("")) {
out.println("<script>alert('请输入密码!');history.back();</script>");
return;
}
if (confirm_password == null || confirm_password.trim().equals("")) {
out.println("<script>alert('请确认密码!');history.back();</script>");
return;
}
if (!password.equals(confirm_password)) {
out.println("<script>alert('两次输入的密码不一致!');history.back();</script>");
return;
}
if (verification == null || verification.trim().equals("")) {
out.println("<script>alert('请输入验证码!');history.back();</script>");
return;
}
HttpSession session = request.getSession();
String verifyCode = (String) session.getAttribute("verification");
if (!verification.equals(verifyCode)) {
out.println("<script>alert('验证码输入错误!');history.back();</script>");
return;
}
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
ps = conn.prepareStatement(SELECT_SQL);
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next() && rs.getInt(1) > 0) {
out.println("<script>alert('用户名已存在!');history.back();</script>");
return;
}
ps = conn.prepareStatement(INSERT_SQL);
ps.setString(1, username);
ps.setString(2, password);
ps.setString(3, role);
ps.executeUpdate();
out.println("<script>alert('注册成功!');location.href='login.jsp';</script>");
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
out.println("<script>alert('注册失败!');history.back();</script>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
```
3. 创建一个名为verification.do的Servlet,生成验证码图片:
```
package com.example;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class VerificationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
int width = 80;
int height = 30;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(new Color(220, 220, 220));
g.fillRect(0, 0, width, height);
g.setColor(new Color(0, 0, 0));
g.setFont(new Font("Arial", Font.BOLD, 20));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
int num = random.nextInt(10);
sb.append(num);
g.drawString(num + "", i * 20 + 5, 25);
}
HttpSession session = request.getSession();
session.setAttribute("verification", sb.toString());
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
4. 在MySQL数据库中创建一个名为users的表,用于存储用户信息:
```
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`role` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
以上是一个简单的示例代码,您可以根据自己的需求进行调整和完善。
阅读全文