[Example of verification code Kaptcha]

1. Configure Maven coordinates

<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->

<dependency>

    <groupId>com.github.penggle</groupId>

    <artifactId>kaptcha</artifactId>

    <version>2.3.2</version>

</dependency>

 

2. Configure Web.xml

 <!-- Configure the verification code plugin servlet class-->

  <servlet>

    <servlet-name>kaptcha</servlet-name>

    <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>

  <!-- Configure parameters for the verification code plugin servlet class -->

    <!-- Border setting yes no -->

    <init-param>

      <param-name>kaptcha.border</param-name>

      <param-value>no</param-value>

    </init-param>

    <!-- Border color color name such as: red blue black, etc. It can also be RGB: 105,179,90 -->

    <init-param>

      <param-name>kaptcha.border.color</param-name>

      <param-value>red</param-value>

    </init-param>

    <!-- border width-->

    <init-param>

      <param-name>kaptcha.border.thickness</param-name>

      <param-value>5</param-value>

    </init-param>

    <!-- The character source of the verification code is compatible with Chinese -->

    <init-param>

      <param-name>kaptcha.textproducer.char.string</param-name>

      <param-value> 1234567890 character source to generate verification code </param-value>

    </init-param>

    <!-- Number of characters to generate result-->

    <init-param>

      <param-name>kaptcha.textproducer.char.length</param-name>

      <param-value>5</param-value>

    </init-param>

    <!-- character font to generate the result -->

    <init-param>

      <param-name>kaptcha.textproducer.font.names</param-name>

      <param-value>Microsoft Yahei,Arial,Italic</param-value>

    </init-param>

    <!-- Character size of the generated result -->

    <init-param>

      <param-name>kaptcha.textproducer.font.size</param-name>

      <param-value>40</param-value>

    </init-param>

    <!-- Character color of the generated result -->

    <init-param>

      <param-name>kaptcha.textproducer.font.color</param-name>

      <param-value>blue</param-value>

    </init-param>

    <!-- Character kerning of the resulting result -->

    <init-param>

      <param-name>kaptcha.textproducer.char.space</param-name>

      <param-value>2</param-value>

    </init-param>

    <!-- Interference Line Generator-->

    <init-param>

      <param-name>kaptcha.noise.impl</param-name>

      <param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value>

    </init-param>

    <!-- Interference line color-->

    <init-param>

      <param-name>kaptcha.noise.color</param-name>

      <param-value>BLACK</param-value>

    </init-param>

    <!-- Interference line background gradient color gradient from left to right -->

    <init-param>

      <param-name>kaptcha.background.clear.from</param-name>

      <param-value>green</param-value>

    </init-param>

    <!-- Interference line background gradient color gradient from right to left -->

    <init-param>

      <param-name>kaptcha.background.clear.to</param-name>

      <param-value>yellow</param-value>

    </init-param>

    <!-- Captcha image width-->

    <init-param>

      <param-name>kaptcha.image.width</param-name>

      <param-value>200</param-value>

    </init-param>

    <!-- Captcha image height-->

    <init-param>

      <param-name>kaptcha.image.height</param-name>

      <param-value>50</param-value>

    </init-param>

  </servlet>

  <!-- CAPTCHA request name mapping

  

  //Get the verification code from the server from the session

        String kaptchaServer = (String)request.getSession().getAttribute

       (com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

  

   -->

  <servlet-mapping>

    <servlet-name>kaptcha</servlet-name>

    <url-pattern>/mykaptcha</url-pattern>

  </servlet-mapping>

 

Third, write jsp pages


 

4. Verification


 

Five, the error solution

严重: Servlet.service() for servlet jsp threw exception

java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest"



 

<!-- Add Servlet -->  

    <dependency>    

        <groupId>javax.servlet</groupId>    

        <artifactId>servlet-api</artifactId>    

        <version>2.5</version>    

          </dependency>

replace with

<!-- Add Servlet -->  

    <dependency>

        <groupId>javax.servlet</groupId>

        <artifactId>javax.servlet-api</artifactId>

        <version>3.1.0</version>

        <scope>provided</scope>

    </dependency>



 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326909061&siteId=291194637
Recommended