JSP驗證碼大全之Servlet實現(二)
???六、Servlet中實現數字英文混合驗證碼
???以下為在Servlet中產生數字跟英文混合驗證碼的代碼分析。
???
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ValidateCodeServlet extends HttpServlet
{
private int x=0;
//設置驗證碼圖片中顯示的字體高度
private int fontHeight;
private int codeY;
//在這里定義了驗證碼圖片的寬度
private int width=60;
//定義驗證碼圖片的高度。
private int height=20;
//定義驗證碼字符個數,此處設置為5位
private int codeNum=5;
char[] codes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ,'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z',};
/**
* 對驗證圖片屬性進行初始化
*/
public void init() throws ServletException
{
//從部署文件web.xml中獲取程序初始化信息,圖片寬度跟高度,字符個數信息
//獲取初始化字符個數
String strCodeNums=this.getInitParameter("codeNum");
//獲取驗證碼圖片寬度參數
String strW=this.getInitParameter("w");
//獲取驗證碼圖片高度參數
String strH=this.getInitParameter("h");
//將配置的字符串信息轉換成數值類型數字
try
{
if(strH!=null && strH.length()!=0)
{
height=Integer.parseInt(strH);
}
if(strW!=null && strW.length()!=0)
{
width=Integer.parseInt(strW);
}
if(strCodeNums!=null && strCodeNums.length()!=0)
{
codeNum=Integer.parseInt(strCodeNums);
}
}
catch(NumberFormatException e)
{}
x=width/(codeNum+1);
fontHeight=height-2;
codeY=height-4;
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
//定義驗證碼圖像的緩沖流
BufferedImage buffImg = new BufferedImage(
width, height,BufferedImage.TYPE_INT_RGB);
//產生圖形上下文
Graphics2D g = buffImg.createGraphics();
//創建隨機數產生函數
Random random = new Random();
//將驗證碼圖像背景填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//創建字體格式,字體的大小則根據驗證碼圖片的高度來設定。
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
//設置字體。
g.setFont(font);
//為驗證碼圖片畫邊框,為一個像素。
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
//隨機生產222跳圖片干擾線條,使驗證碼圖片中的字符不被輕易識別
g.setColor(Color.BLACK);
for(int i = 0; i <222; i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
//randomCode保存隨機產生的驗證碼
StringBuffer randomCode = new StringBuffer();
//定義顏色三素
int red = 0, green = 0, blue = 0;
//隨機生產codeNum個數字驗證碼
for (int i = 0; i < codeNum; i++) {
//得到隨機產生的驗證碼
String strRand = String.valueOf(codeSequence[random.nextInt(82)]);
//使用隨機函數產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
//用隨機產生的顏色將驗證碼繪制到圖像中。
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x, codeY);
//將產生的四個隨機數組合在一起。
randomCode.append(strRand);
}
// 將生產的驗證碼保存到Session中
HttpSession session = req.getSession();
session.setAttribute("validate", randomCode.toString());
// 設置圖像緩存為no-cache。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
//將最終生產的驗證碼圖片輸出到Servlet的輸出流中
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
???七、Servlet驗證碼的部署
????在web.xml中聲明servlet。






















?
鳳凰涅槃/浴火重生/馬不停蹄/只爭朝夕
???? 隱姓埋名/低調華麗/簡單生活/完美人生
posted on 2008-12-04 10:48 poetguo 閱讀(5343) 評論(8) 編輯 收藏 所屬分類: JSP 、JAVA