java学习

java学习

 

验证码的使用



验证码:



1, 导入jarjcaptcha验证码



2.web.xml里配置<servlet>



     <servlet-name>j</servlet-name>



    <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>



   </servlet>



   <servlet-mapping>



     <servlet-name>j</servlet-name>



     <url-pattern>/jcaptcha.jpg</url-pattern>



   </servlet-mapping>



3.在页面上写



<form action="vali.action" method="post">



     <input type="text" name="name"/>



     <a href="javascript:void(0)"
id="mya"><img src="jcaptcha.jpg" id="myimg"/></a>



     <input type="submit" value="save"/>



   </form>



 <script type="text/javascript" src=js/jquery-1.5.1.min.js></script>



 <script type="text/javascript">



       $(document).ready(function(){



          $("#mya").click(function(){



            $("#myimg").attr("src","jcaptcha.jpg?xxx=" + Math.random());



          });



4.struts-vali.xml里写



<?xml version="1.0"
encoding="UTF-8"?>



 <!DOCTYPE struts
PUBLIC



   "-//Apache Software Foundation//DTD Struts
Configuration 2.1.7//EN"



   "http://struts.apache.org/dtds/struts-2.1.7.dtd">



   <struts>



     <package name="myvali"
extends="struts-default">



        <action name="vali" class="com.yjw.web.ValiAction">



       



          <result>WEB-INF/views/list.jsp</result>



          <result name="error">WEB-INF/views/main.jsp?id=1</result>



        </action>



     </package>



   </struts>



5.ValiAction里写:



package com.yjw.web;



 



import org.apache.struts2.ServletActionContext;



import
com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;



import com.opensymphony.xwork2.Action;



 



public class ValiAction implements Action{



   private String name;



   public String execute() throws Exception {



     boolean result = SimpleImageCaptchaServlet.validateResponse(ServletActionContext.getRequest(),
name);



     System.out.println(result);



     if(result){



        return "success";



     }else {



        return "error";



     }



    



   }



   public String getName() {



     return name;



   }



   public void setName(String name) {



     this.name = name;



   }



 



}


posted on 2012-04-28 22:52 杨军威 阅读(362) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜