Struts2实现生成动态验证码并验证实例代码
程序员文章站
2024-03-12 14:14:44
一、基本流程:
产生一个验证码页面(很小)→嵌入到表单中→点击可以刷新页面→表单提交时验证。
二、方法:
1、定义testaction,实现画图方法...
一、基本流程:
产生一个验证码页面(很小)→嵌入到表单中→点击可以刷新页面→表单提交时验证。
二、方法:
1、定义testaction,实现画图方法
package com.zhuguang.action; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.util.map; import javax.servlet.http.httpservletresponse; import org.apache.struts2.interceptor.servletresponseaware; import org.apache.struts2.interceptor.sessionaware; import com.opensymphony.xwork2.actionsupport; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimageencoder; public class testaction extends actionsupport implements sessionaware,servletresponseaware { private map<string, object> session; private httpservletresponse response; private static final long serialversionuid = 1l; private string chknumber; @override public string execute() throws exception { response.setheader("cache-control", "no-cache"); int width=50; //图片宽度 int height=20; //图片高度 bufferedimage image=new bufferedimage(width,height,bufferedimage.type_int_rgb); graphics graphics=image.creategraphics(); graphics.setcolor(this.getcolor()); //背景颜色 graphics.fillrect(0, 0, width, height); graphics.setfont(new font("arial",font.bold,18)); graphics.setcolor(this.getcolor()); //字的颜色 string number=string.valueof(system.currenttimemillis()%9000+1000); //生成四位随机数 session.put("randomcode", number); //写入session中 graphics.drawstring(number, (int)(width*0.1), (int)(height*0.8)); graphics.dispose(); jpegimageencoder encoder=jpegcodec.createjpegencoder(response.getoutputstream()); encoder.encode(image); response.getoutputstream().flush(); //刷新到页面生成图片 response.getoutputstream().close(); //关闭writer return null; } private color getcolor(){ int red=(int)(math.random()*1000%256); int green=(int)(math.random()*1000%256); int blue=(int)(math.random()*1000%256); return new color(red,green,blue); } public string getchknumber() { return chknumber; } public void setchknumber(string chknumber) { this.chknumber = chknumber; } @override public void setsession(map<string, object> session) { // todo auto-generated method stub this.session = session; } @override public void setservletresponse(httpservletresponse response) { // todo auto-generated method stub this.response = response; } }
注意用到session和response
2、在struts.xml文件中注册:
<action name="randomcode" class="com.zhuguang.action.testaction"> </action>
其中不返回任何信息,这样就不会跳转页面
3、jsp页面编写
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!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"> <script type="text/javascript"> <!-- function reloadcode(obj,base){ var rand=new date().gettime(); //这里用当前时间作为参数加到url中,是为了使url发生变化,这样验证码才会动态加载, //只是一个干扰作用,无确实意义,但却又非常巧妙,呵呵 obj.src=base+"randomcode.action?abc="+rand; //其实服务器端是没有abc的字段的。 } //--> </script> <title>测试页面</title> </head> <body> <form action="testlogin" method="post"> username<input type="text" name="name"><br> password<input type="text" name="password"><br> 验证码:<input type="text" name="chknumber" id="chknumber" maxlength="4" class="chknumber_input"> <img title="看不清楚请点击这里" width="50" height="20" src="<%=basepath%>randomcode.action" id="safecode" onclick="reloadcode(this,'<%=basepath%>')" /><br> <input type="submit" value="loginin"> </form> </body> </html>
4、验证
(1)在action中添加一个验证方法
public string testlogin() { if(session.get("randomcode").equals(chknumber)) { return success; } else { return error; } }
(2)在struts.xml中进行注册
<action name="testlogin" class="com.zhuguang.action.testaction" method="testlogin"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action>
以上所述是小编给大家介绍的struts2实现生成动态验证码并验证实例代码,希望对大家有所帮助!
下一篇: Java静态方法不具有多态性详解