浜х敓4涓獙璇佺爜鐨凙CTION錛?/font>
鈥斺斺斺擵alidateCodeAction.java鈥斺斺斺?/font>
package org.struts.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.lang.RandomStringUtils;
public class ValidateCodeAction extends Action {
聽public ActionForward execute(ActionMapping mapping, ActionForm form,
聽聽聽HttpServletRequest request, HttpServletResponse response) {
聽聽try {
聽聽聽int width = 52;
聽聽聽int height = 22;
聽聽聽// 鍙栧緱涓涓?浣嶉殢鏈哄瓧姣嶆暟瀛楀瓧絎︿覆
聽聽聽String code = RandomStringUtils.random(4, true, true);
聽聽聽// 淇濆瓨鍏ession,鐢ㄤ簬涓庣敤鎴風殑杈撳叆榪涜姣旇緝.
聽聽聽// 娉ㄦ剰姣旇緝瀹屼箣鍚庢竻闄ession.
聽聽聽HttpSession session = request.getSession(true);
聽聽聽session.setAttribute("code", code);
聽聽聽response.setContentType("images/jpeg");
聽聽聽response.setHeader("Pragma", "No-cache");
聽聽聽response.setHeader("Cache-Control", "no-cache");
聽聽聽response.setDateHeader("Expires", 0);
聽聽聽ServletOutputStream out = response.getOutputStream();
聽聽聽BufferedImage image = new BufferedImage(width, height,
聽聽聽聽聽BufferedImage.TYPE_INT_RGB);
聽聽聽Graphics g = image.getGraphics();
聽聽聽// 璁懼畾鑳屾櫙鑹?br />聽聽聽g.setColor(getRandColor(200, 250));
聽聽聽g.fillRect(0, 0, width, height);
聽聽聽// 璁懼畾瀛椾綋
聽聽聽Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 璁劇疆瀛椾綋
聽聽聽g.setFont(mFont);
聽聽聽// 鐢昏竟妗?br />聽聽聽// g.setColor(Color.BLACK);
聽聽聽// g.drawRect(0, 0, width - 1, height - 1);
聽聽聽// 闅忔満浜х敓騫叉壈綰匡紝浣垮浘璞′腑鐨勮璇佺爜涓嶆槗琚叾瀹冪▼搴忔帰嫻嬪埌
聽聽聽g.setColor(getRandColor(160, 200));
聽聽聽// 鐢熸垚闅忔満綾?br />聽聽聽Random random = new Random();
聽聽聽for (int i = 0; i < 155; i++) {
聽聽聽聽int x2 = random.nextInt(width);
聽聽聽聽int y2 = random.nextInt(height);
聽聽聽聽int x3 = random.nextInt(12);
聽聽聽聽int y3 = random.nextInt(12);
聽聽聽聽g.drawLine(x2, y2, x2 + x3, y2 + y3);
聽聽聽}
聽聽聽// 灝嗚璇佺爜鏄劇ず鍒板浘璞′腑
聽聽聽g.setColor(new Color(20 + random.nextInt(110), 20 + random
聽聽聽聽聽.nextInt(110), 20 + random.nextInt(110)));
聽聽聽g.drawString(code, 2, 16);
聽聽聽// 鍥捐薄鐢熸晥
聽聽聽g.dispose();
聽聽聽// 杈撳嚭鍥捐薄鍒伴〉闈?br />聽聽聽ImageIO.write((BufferedImage) image, "JPEG", out);
聽聽聽out.close();
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽聽return null;
聽}
聽private Color getRandColor(int fc, int bc) { // 緇欏畾鑼冨洿鑾峰緱闅忔満棰滆壊
聽聽Random random = new Random();
聽聽if (fc > 255)
聽聽聽fc = 255;
聽聽if (bc > 255)
聽聽聽bc = 255;
聽聽int r = fc + random.nextInt(bc - fc);
聽聽int g = fc + random.nextInt(bc - fc);
聽聽int b = fc + random.nextInt(bc - fc);
聽聽return new Color(r, g, b);
聽}
}
struts-config.xml涓厤緗細
聽<action聽 path="/ValidateCode" type="org.struts.action.ValidateCodeAction" />聽
JSP涓皟鐢ㄧ殑鏂規硶錛?/font>
<html:img src="/ValidateCode.do" border="0"
聽聽聽聽onclick="this.src.value='/ValidateCode.do'"
聽聽聽聽alt="璇瘋緭鍏ユ楠岃瘉鐮侊紝濡傜湅涓嶆竻璇風偣鍑誨埛鏂般? style="cursor: pointer"/>