这里使用编码的方式调用kaptcha实现验证码功能
程序运行截图如下:
输入错误后,还是这个界面,但输入正确后:
maven如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>kaptchaDemo2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
</project>
程序结构如下:
源码如下:
LoginServlet.java
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 java.io.IOException;
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String vailCode = request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY).toString().toLowerCase();
String inCode = request.getParameter("inCode").toLowerCase();
if (inCode.equals(vailCode)) {
response.sendRedirect("success.jsp");
} else {
String url = request.getHeader("Referer");
response.sendRedirect(url);
}
}
}
MyServlet.java
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
index.jsp
<%--
Created by IntelliJ IDEA.
User: cff
Date: 2020/2/1
Time: 9:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>kaptchaDemo</title>
<style type="text/css">
.code_a{
color: #0000ff;
font-size: 12px;
text-decoration: none;
cursor: pointer;
}
#imgCode{
cursor: pointer;
}
</style>
<script type="text/javascript">
function changeCode(){
var imgCode = document.getElementById("imgCode");
imgCode.src = "kaptcha?"+Math.random();
}
</script>
</head>
<body>
<form action="login" method="post">
<label>验证码:</label>
<input type="text" id="inCode" name="inCode" />
<img src="kaptcha" align="center" id="imgCode" οnclick="changeCode()" />
<a class="code_a" οnclick="changeCode()">换一张</a><br />
<input type="submit" value="登录"/>
</form>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: cff
Date: 2020/2/1
Time: 9:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>SUCCESS</h1>
</body>
</html>