springMVC Kaptcha 实现验证码功能


6028人阅读 评论(1) 收藏 举报

Kaptcha是一个基于SimpleCaptcha的验证码开源项目。在springMVC环境下,使用kaptcha

    web.xml配置代码:

[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <display-name>display-name>   
  8.     
  9.   <servlet>  
  10.     <servlet-name>helloservlet-name>  
  11.     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  12.     <load-on-startup>1load-on-startup>  
  13.   servlet>  
  14.     
  15.   <servlet-mapping>  
  16.     <servlet-name>helloservlet-name>  
  17.     <url-pattern>/url-pattern>  
  18.   servlet-mapping>  
  19.     
  20.   <servlet>  
  21.     <servlet-name>Kaptchaservlet-name>  
  22.     <servlet-class>com.google.code.kaptcha.servlet.KaptchaServletservlet-class>  
  23.     <init-param>  
  24.         <param-name>kaptcha.borderparam-name>      
  25.         <param-value>noparam-value>  
  26.     init-param>  
  27.     <init-param>  
  28.         <param-name>kaptcha.textproducer.char.spaceparam-name>     
  29.         <param-value>8param-value>  
  30.     init-param>  
  31.     <init-param>  
  32.         <param-name>kaptcha.textproducer.char.lengthparam-name>     
  33.         <param-value>4param-value>  
  34.     init-param>    
  35.   servlet>  
  36.     
  37.   <servlet-mapping>  
  38.     <servlet-name>Kaptchaservlet-name>  
  39.     <url-pattern>/Kaptcha.jpgurl-pattern>  
  40.   servlet-mapping>    
  41.     
  42.   <welcome-file-list>  
  43.     <welcome-file>index.jspwelcome-file>  
  44.   welcome-file-list>  
  45. web-app>  

     hello-servlet.xml配置代码:

[html] view plain copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  9.         http://www.springframework.org/schema/mvc   
  10.         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd  
  11.         http://www.springframework.org/schema/context   
  12.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  13.       
  14.     <context:component-scan base-package="com.lqh.controller" />  
  15.         <mvc:annotation-driven />  
  16.       
  17.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/WEB-INF/jsp/" />       
  19.         <property name="suffix" value=".jsp" />                
  20.     bean>  
  21. beans>  

    controller代码:

[java] view plain copy
  1. @Controller  
  2. public class HelloController {  
  3.       
  4.     @RequestMapping(value={"/""/hello"})  
  5.     public String hello() {  
  6.         System.out.println("hello");  
  7.         return "hello";  
  8.     }  
  9.       
  10.     @RequestMapping(value="/hello", method=RequestMethod.POST)  
  11.     public String hello(String verifyCode, HttpServletRequest request) {  
  12.           
  13.         String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);        //获取生成的验证码  
  14.         System.out.println(verifyCode + "," + code);  
  15.         if(verifyCode.equals(code)) {  
  16.             System.out.println("验证通过 ");  
  17.         }  
  18.         return "redirect:/hello";                                                       //post方式提交,实现客户端跳转  
  19.     }  
  20. }  

       前台页面hello.jsp代码:

[html] view plain copy
  1. <html>  
  2.   <head>  
  3.     <base href="<%=basePath%>">      
  4.     <title>My JSP 'index.jsp' starting pagetitle>   
  5.   head>  
  6.     
  7.   <body>  
  8.     hello.  
  9.     <form method="post">  
  10.         <input type="text" name="verifyCode" />   
  11.         <img alt="验证码" src="Kaptcha.jpg">        

你可能感兴趣的:(springMVC Kaptcha 实现验证码功能)