The content of the above two articles introduced in the JSP generated digital verification code and Chinese Verification Code and analysis of the source code, this article describes how the above generated numbers with the Chinese verification code to use, and how to solve the Chinese verification code garbled is not correct to verify the problem.
The use of the verification code is divided into two parts, respectively, the verification code of the call and verification process, the following are described separately.
Third, call the verification code in the JSP
Use the verification code directly in the picture to generate the code of the JSP file can be, at the same time in the Refresh Verification code button in the JS code in the use of JSP authentication code file, the source page below.
<form id="dForm" method=post action="val.jsp">
<ul class="sFrm">
<li><b></b><input type="button" value="看不清,换一张 " onClick="document.getElementById('code').src='ColorChinese.jsp'"><div class="clear0"></div></li>
<li><b>验证码</b><input type="text" name="input" maxlength=8 value="" styleClass="sIpt itemFm"><div class="clear0"></div></li>
<li class="bar"><b></b><input type="submit" value="验证测试" styleClass="logbtn3"></li>
</ul>
</form>
Four, in the JSP Verification code verification process and the Chinese garbled processing
Get the authentication code entered by the user and compare with the verification code in the session, the same is passed, otherwise refuse, for the processing of Chinese verification code in JSP note to define the JSP page code in the page is consistent with the character encoding to get the session, where the unified GB2312 code is used, Otherwise, the validation code will fail to validate successfully.
val.jsp
<%@ page language="java" pageEncoding="GB2312" %>
<%
request.setCharacterEncoding("gb2312");
//获取Session会话中缓存的验证码
String rand = (String)session.getAttribute("rand");
//获取用户输入的验证码
String input = request.getParameter("input");
if(rand==null||input==null)
{
//无输入验证码处理代码
}
if (rand.equals(input)) {
//验证码对比成功
}
else {
//验证码失败处理代码
}
%>
The above is the validation code in the JSP page generation and the use of processing source code analysis, in the next article will be introduced in the servlet using the CAPTCHA program.