The verification code of JSP code Daquan used and garbled solution

Source: Internet
Author: User
Tags rand

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.