Java tutorial
/** * PureInfo Command * @(#)DrawDigitalImgAction.java 1.0 Aug 23, 2006 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.auth.action; import java.io.IOException; import java.util.Random; import javax.servlet.ServletOutputStream; import org.apache.commons.lang.StringUtils; import org.apache.struts.action.ActionForward; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.common.ImageHelper; public class DrawDigitalImgAction extends ActionBase { protected boolean isLoginNeeded() { return false; } public ActionForward executeAction() throws PureException { response.setContentType("image/png"); Random rand = new Random(); int anInt = rand.nextInt(); if (anInt < 0) anInt = -anInt; anInt = anInt % 10000; String sValidateNum = String.valueOf(anInt); sValidateNum = StringUtils.leftPad(sValidateNum, 4, '0'); try { ServletOutputStream op = response.getOutputStream(); ImageHelper.drawImage(sValidateNum, op); } catch (IOException e) { throw new PureException(0, "", e); } request.getSession().setAttribute("validateNum", sValidateNum); return null; } }