服务器之家

服务器之家 > 正文

Struts2实现生成动态验证码并验证实例代码

时间:2020-05-09 11:54     来源/作者:meaijojo

 一、基本流程:

产生一个验证码页面(很小)→嵌入到表单中→点击可以刷新页面→表单提交时验证。

二、方法:

1、定义TestAction,实现画图方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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文件中注册:

?
1
2
<action name="randomCode" class="com.zhuguang.action.TestAction">
</action>

其中不返回任何信息,这样就不会跳转页面

3、jsp页面编写

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ 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 id="codetool">

4、验证

(1)在Action中添加一个验证方法

?
1
2
3
4
5
6
7
8
9
10
11
public String testLogin()
{
if(session.get("randomCode").equals(chknumber))
{
return SUCCESS;
}
else
{
return ERROR;
}
}

(2)在struts.xml中进行注册

?
1
2
3
4
<action name="testLogin" class="com.zhuguang.action.TestAction" method="testLogin">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>

以上所述是小编给大家介绍的Struts2实现生成动态验证码并验证实例代码,希望对大家有所帮助!

标签:

相关文章

热门资讯

返回顶部