用idea的servlet实现验证码功能
时间: 2023-09-04 12:15:23 浏览: 92
实现验证码功能的主要思路是在用户注册或登录时生成一个随机的验证码图片,并将验证码保存在session中。当用户提交表单时,将用户输入的验证码与session中保存的验证码进行比对,如果一致则继续执行,否则提示用户输入错误。
以下是一个使用IDEA和Servlet实现验证码功能的示例代码。
1. 首先,在web.xml中配置servlet和servlet-mapping:
```xml
<servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>com.example.VerifyCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/verifyCode</url-pattern>
</servlet-mapping>
```
2. 创建一个名为VerifyCodeServlet的servlet类,实现doGet方法:
```java
package com.example;
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;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/verifyCode")
public class VerifyCodeServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 100;
int height = 30;
//创建一个图像缓冲区
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取Graphics2D
Graphics2D g2 = (Graphics2D) image.getGraphics();
//设置背景色
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, width, height);
//设置字体
g2.setFont(new Font("宋体", Font.BOLD, 20));
//生成随机字符
String verifyCode = generateVerifyCode();
//将验证码保存在session中
HttpSession session = request.getSession();
session.setAttribute("verifyCode", verifyCode);
//绘制验证码
Random random = new Random();
for (int i = 0; i < verifyCode.length(); i++) {
g2.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g2.drawString(String.valueOf(verifyCode.charAt(i)), i * 20 + 10, 20);
}
//加入干扰线
for (int i = 0; i < 5; i++) {
g2.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g2.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
}
//输出图像到页面
response.setContentType("image/jpeg");
ImageIO.write(image, "jpeg", response.getOutputStream());
}
/**
* 生成随机验证码
*
* @return
*/
private String generateVerifyCode() {
String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 4; i++) {
sb.append(chars.charAt(random.nextInt(chars.length())));
}
return sb.toString();
}
}
```
3. 在用户注册或登录页面中添加验证码图片和输入框:
```html
<form action="login" method="post">
<div>
<label for="username">用户名:</label>
<input type="text" name="username" id="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" name="password" id="password">
</div>
<div>
<label for="verifyCode">验证码:</label>
<input type="text" name="verifyCode" id="verifyCode">
<img src="verifyCode" alt="验证码">
</div>
<div>
<input type="submit" value="登录">
</div>
</form>
```
4. 在用户登录或注册时验证验证码是否正确:
```java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户输入的验证码
String verifyCode = request.getParameter("verifyCode");
//获取session中保存的验证码
HttpSession session = request.getSession();
String sessionVerifyCode = (String) session.getAttribute("verifyCode");
//比对验证码
if (verifyCode.equalsIgnoreCase(sessionVerifyCode)) {
//验证码正确,继续执行
} else {
//验证码错误,提示用户
}
}
```
以上就是用IDEA和Servlet实现验证码功能的示例代码。
阅读全文