Verification code for simple MAVEN Web project (Jcaptcha component)

Source: Internet
Author: User

A: Jcaptcha introduction

Jcaptcha is an open source Java Open Source component used to generate graphical verification code, which is very easy to use.
JCAPTHCA is very powerful, not only can generate picture-type verification code, but also can generate sound (Sina used a two-factor verification code).
Jcaptcha is a relatively famous project in Captcha.

Two: Switch to generate verification code using Jcaptcha

1: First add the jar package that needs to be used
Note: As part of the jar package Maven Central Library does not need to add itself to the local library, the jar package
The following is the configuration of the jar package in the Pom.xml file

<dependency>      <groupId>Commons-collections</groupId>      <artifactid>Commons-collections</artifactid>      <version>3.0</version>    </Dependency>    <dependency>      <groupId>Commons-logging</groupId>      <artifactid>Commons-logging</artifactid>      <version>1.1.3</version>    </Dependency>    <dependency>      <groupId>Com.octo.captcha</groupId>      <artifactid>Filters</artifactid>      <version>2.0.235</version>    </Dependency>    <dependency>      <groupId>Com.octo.captcha</groupId>      <artifactid>Jcaptcha-api</artifactid>      <version>1.0</version>    </Dependency>    <dependency>      <groupId>Com.octo.captcha</groupId>      <artifactid>Jcaptcha</artifactid>      <version>2.0-alpha-1-snapshot</version>    </Dependency>    <dependency>       <groupId>Com.octo.captcha</groupId>       <artifactid>Jcaptcha-integration-simple-servlet</artifactid>      <version>2.0-alpha-1-snapshot</version>    </Dependency>    <dependency>      <groupId>Javax.servlet</groupId>      <artifactid>Servlet-api</artifactid>      <version>2.4</version>      <scope>Provided</Scope>    </Dependency>

Configuration of the servlet in the Web. xml file, refer to the following address
Https://jcaptcha.atlassian.net/wiki/display/general/Simple+Servlet+Integration+documentation

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "><web-app>    <display-name>Verifycode</display-name>    <servlet>        <servlet-name>Verifycode</servlet-name>        <servlet-class>Com.niu.gao.VerifyCodeServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>Verifycode</servlet-name>        <url-pattern>/verifycode/submit.do</url-pattern>    </servlet-mapping>    <!--jcaptcha configuration https://jcaptcha.atlassian.net/wiki/display/general/Simple+Servlet+Integration+ Documentation --    <servlet>        <servlet-name>Jcaptcha</servlet-name>        <servlet-class>Com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>Jcaptcha</servlet-name>        <url-pattern>/jcaptcha.jpg</url-pattern>    </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></Web-app>

index.jsp page

<%@ page language="java" import="java.util.*" pageencoding="UTF-8"%> <%String path = request. Getcontextpath (); String basepath = request. Getscheme () +"://"+request. getServerName () +":" +request. Getserverport () +path+"/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html>  <head>    <base href="<%=basePath%>">    <title>Verifycode</title>    <meta http-equiv="Pragma" content="No-cache">    <meta http-equiv="Cache-control" content="No-cache" >    <meta http-equiv="Expires" content="0">        <meta http-equiv="keywords" content="Keyword1,keyword2,keyword3" >    <meta http-equiv="description" content="This is my page" >    <script type="text/javascript" src="Jquery-1.9.1.js" ></script>    <script type="Text/javascript"> var path="<%=path%>"; $ (document). Ready (function() { $ (' #codeImg '). On ("click", 
     
      function
      (){ $ (
      ' #codeImg '). Prop (
      ' src ', path+
      '/jcaptch            A.jpg?nocache= '+
      new 
      Date(). GetTime ());        });    });         
     </script>  </head><body Style="Text-align:center;" ><h2>Hello world!</H2><span style="margin:10px 0 0 10px;vertical-align:baseline;" >Please enter a verification code:<input name="Verifycode" type="text" /></span><img id="codeimg" style="vertical-align:bottom;margin:10px 0 0 10px;cursor:pointer; " alt="captcha" src="<%=path%>/jcaptcha.jpg">  </body></html>

Validating the Servlet class

 PackageCom.niu.gao;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;ImportJava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;ImportJavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;/** * @Description: (Verification code servlet) * @author: Relieved * @date: 2015-7-25 */ Public  class verifycodeservlet extends httpservlet {    /** * Serialversionuid * *    Private Static Final LongSerialversionuid =1L@Override    protected void Doget(HttpServletRequest req, HttpServletResponse resp)throwsServletexception, IOException {String codevalue = Req.getparameter ("Verifycode");BooleanCheckrs = Simpleimagecaptchaservlet.validateresponse (req, codevalue); Resp.setcharacterencoding ("GBK"); Resp.setcontenttype ("Text/html"); Resp.getwriter (). Write (");if(CHECKRS) {resp.getwriter (). Write ("verified success!"); }Else{resp.getwriter (). Write ("validation failed!"); } resp.getwriter (). Write ("<br/><a href= ' Javascript:history.go (-1); ' > Try again </a> "); Resp.getwriter (). Write ("</body>); }@Override    protected void DoPost(HttpServletRequest req, HttpServletResponse resp)throwsServletexception, IOException { This. Doget (req, resp); }}



Validation failure diagram

Verify through Diagram

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Verification code for simple MAVEN Web project (Jcaptcha component)

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.