博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用户登陆,退出等基本Action(1) 验证码
阅读量:2453 次
发布时间:2019-05-10

本文共 6412 字,大约阅读时间需要 21 分钟。

首先需要一个验证码生成的工具类(ConfirmCodeGen.java):

package com.tools;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.util.Random;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * 生成验证码 *  * @author Michael Young *  */public final class ConfirmCodeGen {	/**	 * @param response	 * @param width	 * @param height	 * @param len	 * @return	 * @throws Exception	 */	public static String genImageCode(HttpServletResponse response, int width,			int height, int len) throws Exception {		// random number		String code = Integer.toString((int) (Math.random() * (1E4)));		for (int i = code.length(); i < len; i++)			code += "0";		response.setContentType("image/jpeg");		response.addHeader("pragma", "NO-cache");		response.addHeader("Cache-Control", "no-cache");		response.addDateHeader("Expries", 0);		BufferedImage image = new BufferedImage(width, height,				BufferedImage.TYPE_INT_RGB);		Graphics g = image.getGraphics();		// 以下填充背景颜色		g.setColor(Color.WHITE);		g.fillRect(0, 0, width, height);		// 画边框		g.setColor(Color.black);		g.drawRect(0, 0, width - 1, height - 1);		// 设置字体颜色		g.setColor(Color.BLACK);		// g.setColor(new Color());		g.setFont(new Font("Times New Roman", Font.BOLD, 16));		g.drawString(code, 5, 15);		// 88点		Random random = new Random();		for (int i = 0; i < 66; i++) {			int x = random.nextInt(width);			int y = random.nextInt(height);			g.setColor(new Color((int) Math.floor(Math.random() * 255)));			g.drawLine(x, y, x, y);		}		g.dispose();		ServletOutputStream outStream = response.getOutputStream();		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);		encoder.encode(image);		outStream.flush();		outStream.close();		return code;	}	/**	 * @param session	 * @param confirmCode	 * @return	 */	public static boolean isCorrectCode(HttpServletRequest request) {		HttpSession session = request.getSession();		String confirmCode = request.getParameter("confirmCode");		if (session.getAttribute("confirmcode") == null || confirmCode == null) {			return false;		}		String code = (String) session.getAttribute("confirmcode");		// 移除Session		session.removeAttribute("confirmcode");		if (code.equals(confirmCode)) {			return true;		}		return false;	}	/**	 * 从ajax判断验证码的正确与否	 * 	 * @param confirmCode	 *            验证码	 * @param session	 * @return	 */	public static Byte isCorrectCodeFromSession(String confirmCode,			HttpSession session) {		if (session.getAttribute("confirmcode") == null || confirmCode == null) {			return 0;		}		String code = (String) session.getAttribute("confirmcode");		if (code.equals(confirmCode)) {			return 1;		}		// 移除Session		session.removeAttribute("confirmcode");		return 0;	}}

  2、需要一个自定义的Action的result,(ConfirmCodeGenResult.java):

/** *  */package com.web.struts.results;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;import com.tools.ConfirmCodeGen;/** * title: 生成的验证码的Result *  * @author conkeyn * @时间 2009-4-14:下午02:31:52 */public class ConfirmCodeGenResult extends StrutsResultSupport {	/**  */	private static final long serialVersionUID = 2545890371747338210L;	/** 随机图片宽度 */	private int c_width;	/** 随机图片高度 */	private int c_height;	/** 随机数长度 */	private int c_length;	@Override	protected void doExecute(String finalLocation, ActionInvocation invocation)			throws Exception {		HttpServletResponse response = ServletActionContext.getResponse();		HttpServletRequest request = ServletActionContext.getRequest();		HttpSession session = request.getSession();		Object widthO = invocation.getStack().findValue("c_width");		if (widthO != null) {			c_width = (Integer) widthO;			if (c_width <= 0) {				c_width = 45;			}		} else {			c_width = 45;		}		Object heightO = invocation.getStack().findValue("c_height");		if (heightO != null) {			c_height = (Integer) heightO;			if (c_height <= 0) {				c_height = 20;			}		} else {			c_height = 20;		}		Object c_lengthO = invocation.getStack().findValue("c_length");		if (c_lengthO != null) {			c_length = (Integer) c_lengthO;			if (c_length <= 0) {				c_length = 4;			}		} else {			c_length = 4;		}		String random = ConfirmCodeGen.genImageCode(response, c_width,				c_height, c_length);		session.setAttribute("confirmcode", random);	}	public int getC_width() {		return c_width;	}	public void setC_width(int c_width) {		this.c_width = c_width;	}	public int getC_height() {		return c_height;	}	public void setC_height(int c_height) {		this.c_height = c_height;	}	public int getC_length() {		return c_length;	}	public void setC_length(int c_length) {		this.c_length = c_length;	}}

 3、需要一个默认的action():

package com.web.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 基本Action *  *  *  */public class base extends ActionSupport {	/**	 * 	 */	private static final long serialVersionUID = -7546749187705748430L;	/** 验证码的随机图片宽度 */	protected int c_width;	/** 验证码的随机图片高度 */	protected int c_height;	/** 验证码的随机数长度 */	protected int c_length;	public String execute() throws Exception {		return SUCCESS;	}	/**	 * Convenience method to get the request	 * 	 * @return current request	 */	protected HttpServletRequest getRequest() {		return ServletActionContext.getRequest();	}	/**	 * Convenience method to get the response	 * 	 * @return current response	 */	protected HttpServletResponse getResponse() {		return ServletActionContext.getResponse();	}	/**	 * Convenience method to get the session	 */	protected HttpSession getSession() {		return getRequest().getSession();	}		public int getC_width() {		return c_width;	}	public void setC_width(int c_width) {		this.c_width = c_width;	}	public int getC_height() {		return c_height;	}	public void setC_height(int c_height) {		this.c_height = c_height;	}	public int getC_length() {		return c_length;	}	public void setC_length(int c_length) {		this.c_length = c_length;	}}

 配置文件(struts-validation.xml):

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://czdhb.baihongyu.com/

你可能感兴趣的文章
医疗项目 开源_免费和开源医疗保健成功的背后是什么?
查看>>
basic编程命令_从BASIC到Ruby:命令行英雄的第一门编程语言的生活课
查看>>
开源教学系统_通过开源进行教学和口语学习
查看>>
2k19徽章修改_您可以修改此会议徽章
查看>>
命令行python路径命令_探索命令行英雄中Python的过去,现在和未来
查看>>
Codethink开源是入职流程的一部分
查看>>
自动部署 管道 ci cd_每个产品只有一条CI / CD管道来统治它们
查看>>
centos7实验手册_一个小时内创建一个CentOS家庭实验室
查看>>
在命令行英雄的浏览器大战中,JavaScript令人惊讶地崛起
查看>>
vs2015社区_5位社区经理为2015年提供了最大的建议
查看>>
劳德巴赫_众包巴赫杰作的新版本
查看>>
威廉退尔交响曲_容器,微服务和整个交响曲的编排
查看>>
指标口径_使用口径创建每日阅读列表
查看>>
awr报告分析重点_重点摘要和编辑推荐6:2014年12月的报告
查看>>
饥荒更多食物制作mod食谱_您如何看待您附近的更好的食物?
查看>>
微信文章统计点赞数据_前10大文章,统计数据和编辑推荐:2014年10月报告
查看>>
学习杂货店_发展社区杂货店的4个技巧
查看>>
老实人小吃网_3款经典的电脑游戏又回来了(加上小吃!)
查看>>
数据重塑_现在该重塑开源了吗?
查看>>
数据隐私和数据分享_在隐私和开放之间找到数据的最佳位置
查看>>