struts2.0中使用图形验证码

产生随机数action:

package com.cetc.struts.action.t00hy;

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;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.cetc.util.BaseAction;
import com.cetc.util.MyDebug;

public class RandomAction extends BaseAction {

private static final long serialVersionUID = -2269414914034390741L;
    private Random random;
    private BufferedImage image;
    private Font font;
    private int distance;

   
    public String getCheckCodeImage(String str,int show,OutputStream output) throws IOException{
        random = new Random();
        image = new BufferedImage(70,25,BufferedImage.TYPE_3BYTE_BGR);
        font = new Font("Arial",Font.PLAIN,20);
        this.distance = 15;
        Graphics d = image.getGraphics();
        d.setColor(Color.WHITE);
        d.fillRect(0,0,image.getWidth(),image.getHeight());
        d.setColor(new Color(random.nextInt(100)+100,random.nextInt(100)+100,random.nextInt(100)+100));
        for (int i = 0; i < 10; i++) {
            d.drawLine(random.nextInt(image.getWidth()),random.nextInt(image.getHeight()),random.nextInt(image.getWidth()),random.nextInt(image.getHeight()));
        }
        d.setColor(Color.BLACK);
        d.setFont(font);
        String checkCode = "";
        char tmp = 0;
        int x = -distance;
        for (int i = 0; i < show; i++) {
            tmp = str.charAt(random.nextInt(str.length()-1));
            checkCode = checkCode + tmp;
            x = x + distance;
            d.setColor(new Color(random.nextInt(100)+50,random.nextInt(100)+50,random.nextInt(100)+50));
            d.drawString(tmp+"",x,random.nextInt(image.getHeight()-(font.getSize()))+(font.getSize()));
        }
        d.dispose();
        ImageIO.write(image,"jpeg",output);
        return checkCode;
    }



public String execute() throws Exception {
 
  MyDebug.println("-------RandomAction-----------");
  HttpServletResponse response = ServletActionContext.getResponse();
        getSession().put("random",this.getCheckCodeImage("ABCDEFGHJKLMNPQRSTUVWXYZ123456789",4,response.getOutputStream()));
 
  return SUCCESS;
}
 

}

struts配置文件中加入:

<package name="random" extends="struts-default">
  <action name="random" class="com.cetc.struts.action.t00hy.RandomAction">
            <result name="success">/pages/home/random.jsp</result>
        </action>
    </package>

random.jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  <%
   out.clear();
   out = pageContext.pushBody();
   %>


register.jsp页面加入:

   <tr>
   <td class='labeltable_middle_td_01' align=right>验证码</td>
   <td colspan="5" >
    <s:textfield name="num" maxlength="4" cssClass="input" theme="simple" cssStyle="ime-mode:disabled;"/>&nbsp;&nbsp;&nbsp;<a title="看不清吗?换一张吧!"  href='javascript:show(document.getElementById("random"))'><img src="<%=basePath%>random.action" id="random" border="0" /></a>
   </td>
   </tr>



js:

function show(o){
//重载验证码

var timenow = new Date().getTime();
o.src="<%=basePath%>random.action?d="+timenow;
/*
//超时执行;
setTimeout(function(){o.src="<%=basePath%>random.action?d="+timenow;},20);
  */
}

你可能感兴趣的:(java,apache,jsp,struts,servlet)