struts2生成随机验证码图片

package com.xiaoluo.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionSupport;

public class CreateImageAction extends ActionSupport
{
    private ByteArrayInputStream inputStream;

    private static int WIDTH = 60;

    private static int HEIGHT = 20;

    public ByteArrayInputStream getInputStream()
    {
        return inputStream;
    }

    public void setInputStream(ByteArrayInputStream inputStream)
    {
        this.inputStream = inputStream;
    }
private static String createRandom() { String str = "0123456789qwertyuiopasdfghjklzxcvbnm"; char[] rands = new char[4]; Random random = new Random(); for (int i = 0; i < 4; i++) { rands[i] = str.charAt(random.nextInt(36)); } return new String(rands); } private void drawBackground(Graphics g) { // 画背景 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); // 随机产生 120 个干扰点 for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * WIDTH); int y = (int) (Math.random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0); } } private void drawRands(Graphics g, String rands) { g.setColor(Color.BLACK); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); // 在不同的高度上输出验证码的每个字符 g.drawString("" + rands.charAt(0), 1, 17); g.drawString("" + rands.charAt(1), 16, 15); g.drawString("" + rands.charAt(2), 31, 18); g.drawString("" + rands.charAt(3), 46, 16); System.out.println(rands); } @Override public String execute() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); // 设置浏览器不要缓存此图片 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String rands = createRandom(); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 产生图像 drawBackground(g); drawRands(g, rands); // 结束图像 的绘制 过程, 完成图像 g.dispose(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpeg", outputStream); ByteArrayInputStream input = new ByteArrayInputStream(outputStream .toByteArray()); this.setInputStream(input); HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("checkCode", rands); input.close(); outputStream.close(); return SUCCESS; }

}

package com.xiaoluo.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginValidateAction extends ActionSupport
{
    private String checkCode;
    
    public String getCheckCode()
    {
        return checkCode;
    }

    public void setCheckCode(String checkCode)
    {
        this.checkCode = checkCode;
    }

    @Override
    public String execute() throws Exception
    {
        return SUCCESS;
    }
    
    @Override
    public void validate()
    {
        HttpSession session = ServletActionContext.getRequest().getSession();
        
        String checkCode2 = (String)session.getAttribute("checkCode");
        
        if(!checkCode.equals(checkCode2))
        {
            this.addActionError("输入的验证码不正确,请重新输入!");
        }
    }
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    
  </head>
  
  <body>

    <h3><font color="blue">带有验证码的登陆界面</font></h3>
    
    <s:form action="loginValidateAction.action" theme="simple">
    
        用户名:<s:textfield name="username"></s:textfield><br>
        密码    :<s:password name="password"></s:password><br>
        验证码:<s:textfield name="checkCode"></s:textfield>
     
     <!--若要点击图片刷新,重新得到一个验证码,要在后面加上个随机数,这样保证每次提交过去的都是不一样的path,防止因为缓存而使图片不刷新-->
<img src="createImageAction.action" onclick="this.src='createImageAction.action?'+ Math.random()" title="点击图片刷新验证码"/><br> <s:actionerror cssStyle="color:red"/> <s:submit value="提交"></s:submit> </s:form> </body> </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值