我的生成校验码技术--demo(采用java反射技术和dwr技术)
0.准备工作:首先需要导入dwr.jar包
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>demo232</display-name>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="obvalidateCode">
<param name="class" value="com.test.GenerateNumber" />
</create>
</allow>
</dwr>
3.Constants.java
package com.test;
public class Constants {
public static String KEY_1 = "1";
public static String KEY_2 = "2";
public static String KEY_3 = "3";
public static String KEY_4 = "4";
public static String KEY_5 = "5";
public static String KEY_6 = "6";
public static String KEY_7 = "7";
public static String KEY_8 = "8";
public static String KEY_9 = "9";
public static String KEY_10 = "A";
public static String KEY_11 = "B";
public static String KEY_12 = "C";
public static String KEY_13 = "D";
public static String KEY_14 = "E";
public static String KEY_15 = "F";
public static String KEY_16 = "G";
public static String KEY_17 = "H";
public static String KEY_18 = "I";
public static String KEY_19 = "J";
public static String KEY_20 = "K";
public static String KEY_21 = "L";
public static String KEY_22 = "M";
public static String KEY_23 = "N";
public static String KEY_24 = "O";
public static String KEY_25 = "P";
public static String KEY_26 = "Q";
public static String KEY_27 = "R";
public static String KEY_28 = "S";
public static String KEY_29 = "T";
public static String KEY_30 = "U";
public static String KEY_31 = "V";
public static String KEY_32 = "W";
public static String KEY_33 = "X";
public static String KEY_34 = "Y";
public static String KEY_35 = "Z";
public static String COLOR_1 = "orange";
public static String COLOR_2 = "pink";
public static String COLOR_3 = "red";
public static String COLOR_4 = "gray";
public static String COLOR_5 = "blue";
public static String COLOR_6 = "yellow";
}
4.GenerateNumber.java
package com.test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class GenerateNumber {
public List<String> generate(){
List<String> validateCodeList = new ArrayList<String>();
/**生成随机码*/
//产生1-35之间随机数:
for(int i = 0; i < 4; i++){
int x=1+(int)(Math.random()*35);
String key = "KEY_"+x;
System.out.print(x+"\t");
try {
Field field = Constants.class.getField(key);
Object str = "C";
try {
str = field.get((Object)field.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
validateCodeList.add(str.toString());
// System.out.println(field.getName()+"\t"+str.toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
System.out.println();
for(String s : validateCodeList){
System.out.print(s+"\t");
}
System.out.println("\n------------------------");
/**下面生成随机颜色*/
int y=1+(int)(Math.random()*6);
try {
Field field = Constants.class.getField("COLOR_"+y);
try {
Object co = (Object)field.get("COLOR_"+y);
validateCodeList.add(co.toString());
System.out.println(co.toString());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return validateCodeList;
}
public static void main(String[] args) {
new GenerateNumber().generate();
}
}
5.showTest.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>dwr级联显示</title>
<!-- 使用的是相对路径,下面这个引用dwr/interface也是固定写法 -->
<script type='text/javascript' src='dwr/interface/obvalidateCode.js'></script>
<!-- 下面这两个引用是固定写法,因为这两个文件是在dwr.jar中 -->
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript">
function hptest(){
obvalidateCode.generate(show);
}
obvalidateCode.generate(show);
function show(data){
var st = data.toString();
var s = st.split(",");
//alert(s);
for(var i = 0; i < s.length-1; i++){
DWRUtil.setValue("stext_"+i,s[i]);
}
if(s.length > 4){
document.getElementById("bgspan").style.backgroundColor=s[4];
}
}
function checkValidateCode(){
var v1 = DWRUtil.getValue("stext_0");
var v2 = DWRUtil.getValue("stext_1");
var v3 = DWRUtil.getValue("stext_2");
var v4 = DWRUtil.getValue("stext_3");
var v_total = v1+v2+v3+v4;
//alert(v1+","+v2+","+v3+","+v4);
var colValue = document.getElementById("colValue").value;
if(colValue != ""){
if(v_total == colValue){
alert(v_total+"______"+colValue);
alert("校验码输入正确");
}else{
alert(v_total+"______"+colValue);
alert("校验码输入错误");
}
}
}
</script>
</head>
<body>
<form id="myform">
<b>验证码 :</b><input type="text" id="colValue" />
<div id="bgspan" style="width:4%;"><I><b>
<span id="stext_0"></span>
<span id="stext_1"></span>
<span id="stext_2"></span>
<span id="stext_3"></span></b></I>
</div>
<br />
<input type="button" value="validate" onclick="checkValidateCode()" />
</form>
</body>
</html>
|