1、login.jsp页面程序

  1. <script type&#61;"text/Javascript">   
  2. function changeValidateCode(obj) {   
  3. //获取当前的时间作为参数&#xff0c;无具体意义   
  4. var timenow &#61; new Date().getTime();   
  5. //每次请求需要一个不同的参数&#xff0c;否则可能会返回同样的验证码   
  6. //这和浏览器的缓存机制有关系&#xff0c;也可以把页面设置为不缓存&#xff0c;这样就不用这个参数了。   
  7. obj.src&#61;"rand.action?d&#61;"&#43;timenow;   
  8. }   
  9. script> 
  10.  
  11. 在表单中添加下面这句话&#xff1a;  
  12. <s:text name&#61;"random">s:text>&#xff1a;<s:textfield name&#61;"rand" size&#61;"5">s:textfield><img src&#61;"rand.action"  onclick&#61;"changeValidateCode(this)" title&#61;"点击图片刷新验证码"/> 

2、RandomNumUtil.java 生成验证码的类文件

  1. package org.ml.util;  
  2.  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.p_w_picpath.BufferedImage;  
  7. import java.io.ByteArrayInputStream;  
  8. import java.io.ByteArrayOutputStream;  
  9. import java.util.Random;  
  10. import javax.p_w_picpathio.ImageIO;  
  11. import javax.p_w_picpathio.stream.ImageOutputStream;  
  12.  
  13. public class RandomNumUtil {  
  14.     public static final char[] CHARS &#61; { &#39;2&#39;&#39;3&#39;&#39;4&#39;&#39;5&#39;&#39;6&#39;&#39;7&#39;&#39;8&#39;,  
  15.         &#39;9&#39;&#39;A&#39;&#39;B&#39;&#39;C&#39;&#39;D&#39;&#39;E&#39;&#39;F&#39;&#39;G&#39;&#39;H&#39;&#39;J&#39;&#39;K&#39;&#39;L&#39;&#39;M&#39;,  
  16.         &#39;N&#39;&#39;P&#39;&#39;Q&#39;&#39;R&#39;&#39;S&#39;&#39;T&#39;&#39;U&#39;&#39;V&#39;&#39;W&#39;&#39;X&#39;&#39;Y&#39;&#39;Z&#39; };  
  17.     private ByteArrayInputStream p_w_picpath;// 图像  
  18.     private String str;// 验证码  
  19.  
  20.     private RandomNumUtil() {  
  21.         init();// 初始化属性  
  22.     }  
  23.  
  24.     /*  
  25.      * 取得RandomNumUtil实例  
  26.      */ 
  27.     public static RandomNumUtil Instance() {  
  28.         return new RandomNumUtil();  
  29.     }  
  30.  
  31.     /*  
  32.      * 取得验证码图片  
  33.      */ 
  34.     public ByteArrayInputStream getImage() {  
  35.         return this.p_w_picpath;  
  36.     }  
  37.  
  38.     /*  
  39.      * 取得图片的验证码  
  40.      */ 
  41.     public String getString() {  
  42.         return this.str;  
  43.     }  
  44.  
  45.     private void init() {  
  46.         // 在内存中创建图象  
  47.         int width &#61; 85, height &#61; 20;  
  48.         BufferedImage p_w_picpath &#61; new BufferedImage(width, height,  
  49.                 BufferedImage.TYPE_INT_RGB);  
  50.         // 获取图形上下文  
  51.         Graphics g &#61; p_w_picpath.getGraphics();  
  52.         // 生成随机类  
  53.         Random random &#61; new Random();  
  54.         // 设定背景色  
  55.         g.setColor(getRandColor(200250));  
  56.         g.fillRect(00, width, height);  
  57.         // 设定字体  
  58.         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
  59.         // 随机产生155条干扰线&#xff0c;使图象中的认证码不易被其它程序探测到  
  60.         g.setColor(getRandColor(160200));  
  61.         for (int i &#61; 0; i < 155; i&#43;&#43;) {  
  62.             int x &#61; random.nextInt(width);  
  63.             int y &#61; random.nextInt(height);  
  64.             int xl &#61; random.nextInt(12);  
  65.             int yl &#61; random.nextInt(12);  
  66.             g.drawLine(x, y, x &#43; xl, y &#43; yl);  
  67.         }  
  68.         // 取随机产生的认证码(6位数字)  
  69.         StringBuffer sRand &#61; new StringBuffer();    
  70.         for (int i &#61; 0; i < 6; i&#43;&#43;) {  
  71.             String rand &#61; String.valueOf(CHARS[random.nextInt(CHARS.length)]);  
  72.             sRand.append(rand);  
  73.                
  74.             // 将认证码显示到图象中  
  75.             g.setColor(new Color(20 &#43; random.nextInt(110), 20 &#43; random  
  76.                     .nextInt(110), 20 &#43; random.nextInt(110)));  
  77.             // 调用函数出来的颜色相同&#xff0c;可能是因为种子太接近&#xff0c;所以只能直接生成  
  78.             g.drawString(rand, 13 * i &#43; 616);  
  79.         }  
  80.         // 赋值验证码  
  81.         this.str &#61; sRand.toString();  
  82.  
  83.         // 图象生效  
  84.         g.dispose();  
  85.         ByteArrayInputStream input &#61; null;  
  86.         ByteArrayOutputStream output &#61; new ByteArrayOutputStream();  
  87.         try {  
  88.             ImageOutputStream p_w_picpathOut &#61; ImageIO  
  89.                     .createImageOutputStream(output);  
  90.             ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);  
  91.             p_w_picpathOut.close();  
  92.             input &#61; new ByteArrayInputStream(output.toByteArray());  
  93.         } catch (Exception e) {  
  94.             System.out.println("验证码图片产生出现错误&#xff1a;" &#43; e.toString());  
  95.         }  
  96.  
  97.         this.p_w_picpath &#61; input;/* 赋值图像 */ 
  98.     }  
  99.  
  100.     /*  
  101.      * 给定范围获得随机颜色  
  102.      */ 
  103.     private Color getRandColor(int fc, int bc) {  
  104.         Random random &#61; new Random();  
  105.         if (fc > 255)  
  106.             fc &#61; 255;  
  107.         if (bc > 255)  
  108.             bc &#61; 255;  
  109.         int r &#61; fc &#43; random.nextInt(bc - fc);  
  110.         int g &#61; fc &#43; random.nextInt(bc - fc);  
  111.         int b &#61; fc &#43; random.nextInt(bc - fc);  
  112.         return new Color(r, g, b);  
  113.     }  

 

3、RandomAction.java 生成验证码的action程序

  1. import java.io.ByteArrayInputStream;  
  2. import org.ml.util.RandomNumUtil;  
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5. public class RandomAction extends ActionSupport{   
  6. private ByteArrayInputStream inputStream;   
  7. public String execute() throws Exception{   
  8. RandomNumUtil rdnu&#61;RandomNumUtil.Instance();   
  9. this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片   
  10. ActionContext.getContext().getSession().put("random", rdnu.getString());//取得随机字符串放入HttpSession   
  11. return SUCCESS;   
  12. }   
  13. public void setInputStream(ByteArrayInputStream inputStream) {   
  14. this.inputStream &#61; inputStream;   
  15. }   
  16. public ByteArrayInputStream getInputStream() {   
  17. return inputStream;   
  18. }  
  19. }  

4、LoginAction.java 验证验证码的action
 

  1. private String rand; //表单中的rand  
  2. public String getRand() {  
  3. return rand;  
  4. }  
  5. public void setRand(String rand) {  
  6. this.rand &#61; rand;  
  7. }  
  8. //从session中取出RandomAction.java 中生成的验证码random  
  9. String arandom&#61;(String)(ActionContext.getContext().getSession().get("random"));  
  10.  
  11. //下面就是将session中保存验证码字符串与客户输入的验证码字符串对比了  
  12. if(arandom.equals(this.getRand())) {  
  13. ActionContext.getContext().getSession().put("user"this.getUsername());  
  14. return SUCCESS;  
  15. }  
  16. else {  
  17. return ERROR;  
  18. }  

5、配置struts.xml文件
 

  1.  
  2. <action name&#61;"rand" class&#61;"org.ml.rand.RandomAction">     
  3.        <result type&#61;"stream">     
  4.             <param name&#61;"contentType">p_w_picpath/jpegparam>     
  5.             <param name&#61;"inputName">inputStreamparam>     
  6.        result> 
  7.    action> 

演示效果