java验证码(采用struts2实现)转

第一步:编写验证码的Action

 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(4, true, true);
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(200, 250));
44             g.fillRect(0, 0, 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(160, 200));
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, 2, 16);
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

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

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

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>验证码</title>
    <script type="text/javascript" src="js/jquery-1.6.2.min.js">
    </script>
    <script type="text/javascript">
    function changeImg(){     
        $("#authCode").attr("src","test/authCode.action?d="+new Date().valueOf());     
    }    
    </script>
  </head>
  
  <body>
  <center>
           <br>
           <img src="test/authCode.action" alt="验证码" id="authCode" onclick="changeImg()"> 
          <a href="#" onclick="changeImg()">看不清,换一张!</a> 
  </center>
  </body>
</html>
原文地址:https://www.cnblogs.com/xingmeng/p/3287516.html