Struts2学习7-(验证码)

1、生成验证码

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

import cn.dsna.util.images.ValidateCode;

/**
 * 通过配置文件调整生成文件的大小
 * @author linoer
 *	自定义结果类型
 *		1、继承StrutsResultSupport,重写doExecute方法
 */
public class MyResult extends StrutsResultSupport{
	private int width;
	private int height;
	private int numStyle;
	private int lineNum;
	/* (non-Javadoc)
	 * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
	 */
	@Override
	protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
		// TODO Auto-generated method stub
		//使用第三方生成验证码的jar包
		/*
		 * 1、拷贝jar包
		 * 2、创建ValidateCode对象
		 * 3、获取响应对象输出流
		 * 4、输出到浏览器
		 */
		//参数详解:1、图像宽高度,2、数字格式,3、干扰条数
		ValidateCode code = new ValidateCode(width,height,numStyle,lineNum);
		//获取响应对象
		HttpServletResponse response = ServletActionContext.getResponse();
		//输出到浏览器
		code.write(response.getOutputStream());
		//	ImageIO.write(image, "jpg", response.getOutputStream());
		
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getNumStyle() {
		return numStyle;
	}
	public void setNumStyle(int numStyle) {
		this.numStyle = numStyle;
	}
	public int getLineNum() {
		return lineNum;
	}
	public void setLineNum(int lineNum) {
		this.lineNum = lineNum;
	}
}




2、xml配置

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎登陆</title>
</head>
<body>
	<form action="" method="post">
		用户名:<input type="text" name="username"><br/>
		密码:<input type="password" name="password"><br/>
		验证码:<input type="text" name="valicateCode">
		<img src="${pageContext.request.contextPath}/captchaAction.action">
		<input type="submit" value="登陆"/>
	</form>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值