java验证码(采用struts2实现)

来源:互联网 发布:网络理财收益排行榜 编辑:程序博客网 时间:2024/06/14 20:18

第一步:编写验证码的Action

[java] view plaincopy
  1. package com;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.util.Random;  
  8.   
  9. import javax.imageio.ImageIO;  
  10. import javax.servlet.ServletOutputStream;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import javax.servlet.http.HttpSession;  
  14.   
  15. import org.apache.commons.lang.RandomStringUtils;  
  16. import org.apache.struts2.ServletActionContext;  
  17. public class AuthCodeAction {  
  18.     private HttpServletResponse response = ServletActionContext.getResponse();  
  19.     private HttpServletRequest request = ServletActionContext.getRequest();  
  20.   
  21.     public String execute() {  
  22.         try {  
  23.             int width = 50;  
  24.             int height = 18;  
  25.             // 取得一个4位随机字母数字字符串  
  26.             String s = RandomStringUtils.random(4truetrue);  
  27.   
  28.             // 保存入session,用于与用户的输入进行比较.  
  29.             // 注意比较完之后清除session.  
  30.             HttpSession session = request.getSession(true);  
  31.             session.setAttribute("authCode", s);  
  32.   
  33.             response.setContentType("images/jpeg");  
  34.             response.setHeader("Pragma""No-cache");  
  35.             response.setHeader("Cache-Control""no-cache");  
  36.             response.setDateHeader("Expires"0);  
  37.   
  38.             ServletOutputStream out = response.getOutputStream();  
  39.             BufferedImage image = new BufferedImage(width, height,  
  40.                     BufferedImage.TYPE_INT_RGB);  
  41.             Graphics g = image.getGraphics();  
  42.             // 设定背景色  
  43.             g.setColor(getRandColor(200250));  
  44.             g.fillRect(00, width, height);  
  45.   
  46.             // 设定字体  
  47.             Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体  
  48.             g.setFont(mFont);  
  49.   
  50.             // 画边框  
  51.             // g.setColor(Color.BLACK);  
  52.             // g.drawRect(0, 0, width - 1, height - 1);  
  53.   
  54.             // 随机产生干扰线,使图象中的认证码不易被其它程序探测到  
  55.             g.setColor(getRandColor(160200));  
  56.             // 生成随机类  
  57.             Random random = new Random();  
  58.             for (int i = 0; i < 155; i++) {  
  59.                 int x2 = random.nextInt(width);  
  60.                 int y2 = random.nextInt(height);  
  61.                 int x3 = random.nextInt(12);  
  62.                 int y3 = random.nextInt(12);  
  63.                 g.drawLine(x2, y2, x2 + x3, y2 + y3);  
  64.             }  
  65.   
  66.             // 将认证码显示到图象中  
  67.             g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  68.                     .nextInt(110), 20 + random.nextInt(110)));  
  69.   
  70.             g.drawString(s, 216);  
  71.   
  72.             // 图象生效  
  73.             g.dispose();  
  74.             // 输出图象到页面  
  75.             ImageIO.write((BufferedImage) image, "JPEG", out);  
  76.             out.close();  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         }  
  80.         return null;  
  81.     }  
  82.   
  83.     private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色  
  84.         Random random = new Random();  
  85.         if (fc > 255)  
  86.             fc = 255;  
  87.         if (bc > 255)  
  88.             bc = 255;  
  89.         int r = fc + random.nextInt(bc - fc);  
  90.         int g = fc + random.nextInt(bc - fc);  
  91.         int b = fc + random.nextInt(bc - fc);  
  92.         return new Color(r, g, b);  
  93.     }  
  94. }  

第二步:配置action

[html] view plaincopy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.     <package name="test" namespace="/test" extends="struts-default">  
  5.         <action name="authCode" class="com.AuthCodeAction" method="execute">  
  6.         </action>  
  7.     </package>  
  8. </struts> </span><span style="font-size: 18px;">   </span>  

第三步:编写jsp页面(采用jquery实现动态刷新)

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.     <title>验证码</title>  
  12.     <script type="text/javascript" src="js/jquery-1.6.2.min.js">  
  13.     </script>  
  14.     <script type="text/javascript">  
  15.     function changeImg(){       
  16.         $("#authCode").attr("src","test/authCode.action?d="+new Date().valueOf());       
  17.     }      
  18.     </script>  
  19.   </head>  
  20.     
  21.   <body>  
  22.   <center>  
  23.          <br>  
  24.          <img src="test/authCode.action" alt="验证码" id="authCode" onclick="changeImg()">   
  25.         <a href="#" onclick="changeImg()">看不清,换一张!</a>   
  26.   </center>  
  27.   </body>  
  28. </html>  

0 0
原创粉丝点击