java验证码(采用struts2实现)转
生活随笔
收集整理的這篇文章主要介紹了
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" οnclick="changeImg()"> <a href="#" οnclick="changeImg()">看不清,換一張!</a> </center></body> </html>?
?
?
總結
以上是生活随笔為你收集整理的java验证码(采用struts2实现)转的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 决策树模型组合之随机森林与GBDT
- 下一篇: java自学感悟