在登录页面上添加验证码

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

创建一个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+'?'">

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

相关推荐
RestCloud1 天前
ETL、ELT、CDC区别详解,2026数据集成模式选型指南与落地实战
数据仓库·etl·cdc·数据同步·数据集成平台·实时数据同步·elt
江晓鱼未暖1 天前
十七、Redis 核心原理与架构详解
大数据·数据库·数据仓库·redis·缓存·架构
l1t1 天前
DeepSeek总结的上下文理应归属于数据仓库
数据仓库
牛奶咖啡134 天前
大数据Hadoop运维应用实践——HIVE与Hadoop实现整合_Hive的配置安装与使用
大数据·hive·hive的配置与安装·metastore服务的配置·hiveserver2服务配置·hive的常用sql操作·beeline的使用
Zhu7585 天前
在k8s集群环境部署高可用的Apache Hadoop3.1.1定制版集群
hadoop·kubernetes
Y3815326625 天前
3 种 SERP 数据 ETL 方案对比:实时 / 定时 / 流式
数据仓库·etl
还有你Y6 天前
软件工程架构
hadoop·架构·软件工程
向夏威夷 梦断明暄6 天前
基于Tez引擎的 Hive SQL 性能优化
hive·sql·性能优化
这个DBA有点耶7 天前
交易型数据库是什么?OLTP核心能力与2026选型指南
数据库·数据仓库·sql·database·数据库架构·olap·dba