javaweb实现用户注册登录页面,并完成图片验证码的代码
时间: 2023-06-18 08:04:35 浏览: 128
以下是一个使用JavaWeb实现用户注册登录页面并完成图片验证码的示例代码:
1. 注册页面代码 register.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="register" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="confirm_password"><br>
验证码:<input type="text" name="verificationCode"><img src="verificationCode" alt="验证码"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
2. 登录页面代码 login.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
验证码:<input type="text" name="verificationCode"><img src="verificationCode" alt="验证码"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
3. 图片验证码生成代码 VerificationCodeServlet.java
```java
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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/verificationCode")
public class VerificationCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width = 80;
int height = 40;
Random random = new Random();
// 随机生成验证码
String verificationCode = "";
for (int i = 0; i < 4; i++) {
verificationCode += random.nextInt(10);
}
// 将验证码存入session
HttpSession session = request.getSession();
session.setAttribute("verificationCode", verificationCode);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 设置背景颜色
g.setColor(new Color(255, 255, 255));
g.fillRect(0, 0, width, height);
// 设置字体颜色
g.setColor(new Color(0, 0, 0));
// 设置字体
g.setFont(new Font("Arial", Font.BOLD, 20));
// 绘制验证码
g.drawString(verificationCode, 20, 25);
// 输出图片
response.setContentType("image/jpeg");
ImageIO.write(image, "jpeg", response.getOutputStream());
}
}
```
4. 注册和登录处理代码 RegisterServlet.java 和 LoginServlet.java
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmPassword = request.getParameter("confirm_password");
String verificationCode = request.getParameter("verificationCode");
HttpSession session = request.getSession();
String sessionVerificationCode = (String) session.getAttribute("verificationCode");
if (!verificationCode.equals(sessionVerificationCode)) {
// 验证码错误
response.sendRedirect("register.jsp");
} else if (!password.equals(confirmPassword)) {
// 两次密码不一致
response.sendRedirect("register.jsp");
} else {
// 注册成功
// ...处理注册逻辑
response.sendRedirect("login.jsp");
}
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String verificationCode = request.getParameter("verificationCode");
HttpSession session = request.getSession();
String sessionVerificationCode = (String) session.getAttribute("verificationCode");
if (!verificationCode.equals(sessionVerificationCode)) {
// 验证码错误
response.sendRedirect("login.jsp");
} else {
// 登录成功
// ...处理登录逻辑
response.sendRedirect("index.jsp");
}
}
}
```
注意:以上代码仅为示例代码,实际应用中需要根据具体需求进行修改和完善。
阅读全文