在登录页面上添加验证码

这是一个简单的登录页面,里面必须通过验证码通过之后才能够进入页面

创建一个servlet(验证码的)

java 复制代码
package qcby.util;

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;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;

/**
 * Servlet implementation class Test
 */
@WebServlet("/CaptchaServlet")
public class CaptchaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CaptchaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//定义图形验证码的长、宽、验证码字符数、干扰元素个数
		CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
		
		System.out.println(captcha.getCode());
		HttpSession session = request.getSession();
		session.setAttribute("captchaVal", captcha.getCode());
		
		//输出到浏览器上
		captcha.write(response.getOutputStream());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

在之前创建过的js中实现它

javascript 复制代码
$(".btn").on("click",function(){
	var account = $(".account").val().trim()
	var password = $(".password").val().trim()
	var captcha = $(".captcha").val().trim()
	$.ajax({
		url:"Studentlogin",
		type:"get",
		data:{
			account,
			password,
			captcha
		},
		success:function(value){
			alert(value)
			if(value=="登录成功"){
				location.href="student.html"
			}
			if(value=="验证码错误"){
				$("img").click()
				$(".captcha").val("")
			}
		}
	})
})

在html中也加入相关内容

html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="js/jquery-3.7.1.min.js"></script>
<script src="js/jquery.cookie.min.js"></script>
<script src="js/login.js" defer></script>
</head>
<body>

	<div>
		账号:<input type='text' class='account'><br>
		密码:<input type='text' class='password'><br>
		验证码:<input type='text' class='captcha'><img src="CaptchaServlet" width="100" onclick="this.src=this.src+'?'"><br>
		<input type='button' value='登录' class='btn'> 
	</div>
</body>
</html>

<img src="CaptchaServlet" width="100" οnclick="this.src=this.src+'?'">

这里使调整图片的大小和验证码的来源,以及点击验证码的图片就可以刷新,避免看不清验证码无法输入的问题

相关推荐
衡石科技3 天前
二十多种数据源接进来:适配层、方言重写与 ETL 管道工程
数据仓库·etl·数据集成
skywalk81634 天前
lightharness第一次大规模开hive任务卡成功
人工智能·hive·agent·deepseek·lightharness
大今野4 天前
2026 专转本计算机|大数据关键技术:Hadoop、MapReduce、NoSQL 与处理流程(附例题)
大数据·hadoop·mapreduce·专转本
SeanQhl5 天前
鲲鹏开发套件综合实战
hadoop·性能调优·openeuler·鲲鹏devkit·应用迁移
崖边看雾5 天前
Hive 手册:建表、分区、查询与调优实战指南
数据仓库·hive·hadoop
AllData公司负责人6 天前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
CoderYanger8 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
starzy199011 天前
Structured Streaming 模块与编程模型:引擎内部到底由什么组成
大数据·hadoop·hdfs
AI源码12 天前
基于 Hadoop 和 Hive 的旅游景区数据的分析与可视化系统
hive·hadoop·旅游
DataY轻量数据平台14 天前
任务调度的三种模式:什么时候跑、按什么顺序跑、等谁跑完
数据仓库·etl