https://www.cnblogs.com/wypzpz/p/12459370.html中有生成验证码的具体代码
前端
$(function () {
$("#verifycode").mouseout(function () {
$.get("${pageContext.request.contextPath}/checkCodeServlet",
{"checkCode":$("#verifycode").val()},
function (data) {
if (data=="false"){
checkCode_flag = false;
$("#errorCode").attr("hidden",false);
}
if (data=="true"){
checkCode_flag =true;
$("#errorCode").attr("hidden",true);
}
}
)
})
后端
package 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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String checkCode = (String) session.getAttribute("CHECKCODE");
String inputCheckCode = request.getParameter("checkCode");
if (checkCode.equalsIgnoreCase(inputCheckCode)){
response.getWriter().write("true");
}else {
response.getWriter().write("false");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}