maven
<dependency>
<groupId>com.bladejava</groupId>
<artifactId>blade-patchca</artifactId>
<version>1.0.3</version>
</dependency>
controller
package com.kaishengit.web;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.font.FontFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;
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.awt.*;
import java.io.IOException;
import java.io.OutputStream;
@WebServlet("/patchca.png")
public class PatchcaServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
cs.setColorFactory(new SingleColorFactory(new Color(11, 32, 170)));
cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
RandomWordFactory wordFactory = new RandomWordFactory();
wordFactory.setMinLength(4);
wordFactory.setMaxLength(4);
//wordFactory.setCharacters("0123456789中了深刻的房间啦师傅拉萨扩大就卢卡斯的flak时间的flask的距离喀什的加拉塞克地方");
cs.setWordFactory(wordFactory);
FontFactory factory = new FontFactory() {
@Override
public Font getFont(int i) {
return new Font("微软雅黑",Font.BOLD,34);
}
};
cs.setFontFactory(factory);
//!!!!!!!!!!!!!!! 通过Response对象获取响应输出流
OutputStream outputStream = resp.getOutputStream();
String code = EncoderHelper.getChallangeAndWriteImage(cs,"png",outputStream);
//将产生的验证码放入session
HttpSession session = req.getSession();
session.setAttribute("patchca",code);
outputStream.flush();
outputStream.close();
}
}
跳转的页面
package com.kaishengit.web;
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;
@WebServlet("/pay")
public class PayServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/views/pay.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String code = req.getParameter("code");
String money = req.getParameter("money");
//验证验证码是否正确
HttpSession session = req.getSession();
String sessionCode = (String) session.getAttribute("patchca");
if(code != null && code.equals(sessionCode)) {
session.removeAttribute("patchca");
System.out.println("支付:" + money + "元");
} else {
req.setAttribute("message","验证码错误");
req.setAttribute("money",money);
req.getRequestDispatcher("/WEB-INF/views/pay.jsp").forward(req,resp);
}
}
}
页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#change {
text-decoration: none;
display: block;
width: 160px;
height: 70px;
}
</style>
</head>
<body>
<h3>${message}</h3>
<form action="/pay" method="post">
金额:<input type="text" name="money" value="${money}">
<br>
验证码:<input type="text" name="code">
<br>
<a href="javascript:;" id="change">
![](/patchca.png)
</a>
<br>
<button>支付</button>
</form>
<script src="/static/js/jquery-1.11.3.min.js"></script>
<script>
$(function () {
$("#change").click(function () {
$("#img").removeAttr("src").attr("src","/patchca.png?_="+new Date().getTime());
});
});
</script>
</body>
</html>