1.首先要有生成验证码图片的代码
package info.frady.result;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class VerifyImage
{
public String GetImage(OutputStream outputStream)
{
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
String sRand = "";
for (int i = 0; i < 4; i++)
{
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random
.nextInt(110)));
g.drawString(rand, 13 * i + 6, 16);
}
g.dispose();
try
{
ImageIO.write(image, "JPEG", outputStream);
outputStream.flush();
return sRand;
} catch (IOException e)
{
e.printStackTrace();
return "fail";
}
}
public Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
2.自定义一个result,来调用生成验证码的代码,并将验证码写入session
package info.frady.result;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;
import com.opensymphony.xwork2.ActionInvocation;
public class ImageResult extends StrutsResultSupport{
private HttpSession session;
protected void doExecute(String arg0, ActionInvocation invocation) throws Exception
{
//获得Request
HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(
ServletActionContext.HTTP_REQUEST);
//获得session
session = request.getSession(true);
//获得response
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(
ServletActionContext.HTTP_RESPONSE);
//设置无缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
VerifyImage verify = new VerifyImage();
OutputStream os = response.getOutputStream();
//获得随机验证码
String str = verify.GetImage(os);
//将验证码放入session
if(session.getAttribute("rand")!=null){
session.removeAttribute("rand");
}
session.setAttribute("rand", str);
}
}
3.使用此验证码,在action中配置
<result-types>
<result-type name="image" class="info.frady.result.ImageResult" />
</result-types>
<action name="image" class="info.frady.action.ImageAction">
<result name="success" type="image" />
</action>
4.当然了,有人会问ImageAction的代码是什么,答案是空的
package info.frady.action;
import com.opensymphony.xwork2.ActionSupport;
public class ImageAction extends ActionSupport {
public String execute() throws Exception {
return SUCCESS;
}
}
使用的话,就用image.action来访问。。。。
分享到:
相关推荐
在struts2下产生验证码,包含所需要的action(返回的就是验证码图片),struts配置文件,页面的调用。
生成验证码图片主要涉及到两个类: 1.SecurityCode.java --- 生成验证码 SecurityCode类中提供的验证码分三个难度,易(全数字)、中(数字+小写英文)、难(数字+大小写英文)。难度使用枚举SecurityCodeLevle表示...
1.SecurityCode.java --- 生成验证码 SecurityCode类中提供的验证码分三个难度,易(全数字)、中(数字+小写英文)、难(数字+大小写英文)。难度使用枚举SecurityCodeLevle表示,避免使用1、2、3这样没有明确意义...
Struts2的验证码生成
struts2生成中文验证码的Action. 非常的简单,也很实用
struts2生成图片验证码
基于struts2的验证码,点击图片更新验证码,异步检查验证码是否正确..
通过ajax来动态的验证验证码输入是否正确。我们这里采用的是ajax+struts2来做的这个验证。...之后我们需要写一个类来生成验证码。 博客地址:http://blog.csdn.net/sdksdk0/article/details/51755489
主要是用strus2实现验证码功能,里面包含验证码的实现以及xml文件里面的action的书写
验证码的生成,源代码,供参考
一个登录验证生成验证码用struts 后台生成验证码在jsp页面输出生成图片
简单轻巧的验证码生成工具类。
主要介绍了Struts2实现生成动态验证码并验证实例代码的相关资料,非常不错具有参考借鉴价值,需要的朋友可以参考下
使用struts的流Action响应生成一个验证码返回给客户端。 可以练习struts的使用,欢迎各位下载交流。
一个使用struts使用struts2.0实现的验证码功能的登录页面,可以方便的生成验证码
struts2自己生成验证码的一个完整项目案例,包含了生成的jar包。总结了两天,希望和大家分享!
Struts生成的验证码小demo,既简单又实用啊
NULL 博文链接:https://lpf.iteye.com/blog/1532680
这里命名为01_image.jsp,这类的主要功能就是生成验证码,里面是各种画线条,随机数字等,我这里设置的是5个数字的验证,如果你想要改成其他的也可以,大致思路就是在生成数字的那个循环那里加上字母就可以了。...
from表单,生成验证码(纯数字、纯字母、数字加字母)_ajax--------struts版