验证码的使用
验证码:
1, 导入jar包jcaptcha验证码
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;
}
}