实现效果
点击看不清,换一张图片,会换一张图片 实现原理: 继承HttpServlet使用CaptchaUtil.createCircleCaptcha()方法 注意: http://localhost:8080/jspday05/gc?i=1 这个i是可以变换的,没变一次,图片改变一次
html页面
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<%
String path
= request
.getContextPath();
String basePath
= request
.getScheme() + "://" + request
.getServerName() + ":" + request
.getServerPort()
+ path
+ "/";
%>
<head>
<title>Title
</title
>
</head
>
<body>
<div>
<table border
="0" cellspacing
="5" cellpadding
="5" >
<tr>
<td> <div id
="checkCode" class="code" ></div
></td
>
<td> <img id
="im" src
="http://localhost:8080/jspday05/gc"> <a id
="kanbuq" href
="javascript:changeImg();">看不清,换一张
</a
> </div
></span
></td
>
</tr
>
<tr>
<td>验证码:
</td
>
<td><input type
="text" id
="inputCode" style
="float:left;" /></td
>
</tr
>
<tr>
<td></td
>
<td><input type
="button" onclick
="validateCode()" value
="确定" /></td
>
</tr
>
</table
>
</div
>
<script src
="js/jquery-3.5.0.min.js" charset
="utf-8"></script
>
<script>
var i
=0
function
changeImg() {
$
("#im").attr("src","http://localhost:8080/jspday05/gc?i="+i
);
i
++;
}
function
validateCode(){
$
.ajax({
type
:"get",
url
:"<%=basePath%>tt",
dataType
:"text",
data
:{
code
:$
("#inputCode").val()
},
success
:function
(info
) {
alert(info
);
},
error
:function
(info
) {
alert(info
);
}
})
}
</script
>
</body
>
</html
>
引入工具包创建这个验证码
使用hutool-all-4.6.1.jar这个工具包
import cn
.hutool
.captcha
.CaptchaUtil
;
import cn
.hutool
.captcha
.CircleCaptcha
;
import javax
.servlet
.ServletException
;
import javax
.servlet
.ServletOutputStream
;
import javax
.servlet
.annotation
.WebServlet
;
import javax
.servlet
.http
.HttpServlet
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
import java
.io
.IOException
;
@WebServlet("/gc")
public class CreateCapact extends HttpServlet {
@Override
protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
CircleCaptcha captcha
= CaptchaUtil
.createCircleCaptcha(110, 40, 4, 10);
ServletOutputStream sos
= resp
.getOutputStream();
captcha
.write(sos
);
String code
= captcha
.getCode();
System
.out
.println(code
);
req
.getSession().setAttribute("code",code
);
sos
.flush();
sos
.close();
}
}
测试效果类
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
.io
.IOException
;
import java
.io
.PrintWriter
;
@WebServlet("/tt")
public class TestAjax extends HttpServlet {
@Override
protected void service(HttpServletRequest req
, HttpServletResponse resp
) throws ServletException
, IOException
{
System
.out
.println("进来了");
resp
.setContentType("text/html; charset=UTF-8");
String code9
= req
.getParameter("code");
System
.out
.println(code9
);
Object code1
= req
.getSession().getAttribute("code");
String code2
=(String
)code1
;
PrintWriter pw
= resp
.getWriter();
if (code9
.equals(code2
)){
pw
.println("验证码正确");
System
.out
.println("验证码正确");
}else {
System
.out
.println("验证码失败");
pw
.println("验证码错误");
}
}
}