当前位置:网站首页>Kaptcha generates verification code on Web page

Kaptcha generates verification code on Web page

2022-07-08 01:18:00 Programmer community


kaptcha It's an extension from simplecaptcha Verification code library , It is convenient for us to stop writing such functions .
His code is hosted by Google , You can download... From here http://code.google.com/p/kaptcha/
Examples have been attached to the project , It can be used by developers .

 

The operation required is to kaptcha-2.3.2.jar Added to the project , Then configure the Servlet:
The configuration in the example is :

<servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/Kaptcha.jpg</url-pattern></servlet-mapping>

That is to say, visit Kaptcha.jpg In fact, I accessed the output verification code image Servlet.
In the example KaptchaExample.jsp Is the call page , Here is how to verify whether the user input conforms to the verification code .
But there is no operation of how to refresh , Refreshing is actually very simple , Here you can modify it slightly .
Be careful , Random parameters must be added , Otherwise, reading the cache will not be effective .

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html> <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>Kaptcha Example</title>  <script type="text/javascript">  function refImg(){   document.getElementById("Kaptcha").src="<%=basePath%>Kaptcha.jpg?data="+Math.random();  }  </script> </head> <body>  <table>   <tr>    <td><img id="Kaptcha" src="<%=basePath%>Kaptcha.jpg" onclick="refImg()"></td>    <td valign="top">     <form method="POST">      <br>sec code:<input type="text" name="kaptchafield"><br />      <input type="submit" name="submit">     </form>    </td>   </tr>  </table>  <br />  <%   String c = (String)session.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);   String parm = (String) request.getParameter("kaptchafield");   out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");   if (c != null && parm != null) {    if (c.equals(parm)) {     out.println("<b>true</b>");    } else {     out.println("<b>false</b>");    }   }  %> </body></html>

In addition, the absolute path is used after modification , We should pay attention to this problem in actual development .
As an example, the kaptcha-2.3.2, See the attachment .

 

Click download attachment

I recommend you to read more about “ Google servlet Open source Verification Code kaptcha ” The article

原网站

版权声明
本文为[Programmer community]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130545375472.html