导入kaptcha的jar包
配置文件
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
servlet
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.awt.image.BufferedImage;
import java.io.IOException;
@WebServlet("/judgecode")
public class JudgeCode extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String code = (String) req.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
String usercode = req.getParameter("usercode");
System.out.println(code + " " +usercode);
if(code.equals(usercode)){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/judgecode">
验证码<input type="text" name = "usercode">
<img src="/kaptcha.jpg" alt="">
<input type="submit" value="提交">
</form>
</body>
</html>
I'm so cute. Please give me money.
- 本文链接:http://yoursite.com/2020/06/02/Java%E4%BD%BF%E7%94%A8%E6%8F%92%E4%BB%B6%E7%94%9F%E6%88%90%E9%AA%8C%E8%AF%81%E7%A0%81/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub IssuesGitHub Discussions