JSP彩色驗證碼的實現!
具體的實現情況請看我們的論壇()的驗證碼~~~~ http://bbs.8isp.cn
http://bbs.8isp.cn
先把思路說下: http://bbs.8isp.cn
http://bbs.8isp.cn
一個JSP頁面或者HTML頁面(A.JSP)調用一個SERVLET而實現驗證碼,SERVLET負責把圖象里的數字保存到SESSION中,在JSP或者HTML的頁面中把值傳遞給B.jsp,B.JSP負責接收A傳遞過來的值和SESSION里的值做比較如果一樣就說明用戶輸入正確的驗證碼,如果不一樣就返回個錯誤! http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
好了現在我們開始了,,, http://bbs.8isp.cn
http://bbs.8isp.cn
先寫個SERVLET,注意包的路徑我的路徑為:WEB-INF\classes\dreamtime\dreamnews\ImageServlet.java http://bbs.8isp.cn
http://bbs.8isp.cn
先看看這個SERVLET:
package com.semovy.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ImageServlet extends HttpServlet {
// 驗證碼圖片的寬度。
private int width=60;
//驗證碼圖片的高度。
private int height=20;
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("times new roman",Font.PLAIN,18);
//設置字體。
g.setFont(font);
//畫邊框。
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//隨機產生160條干擾線,使圖象中的認證碼不易被其它程序探測到。
g.setColor(Color.gray);
for (int i=0;i<10;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;
//隨機產生4位數字的驗證碼。
for (int i=0;i<4;i++)
{
//得到隨機產生的驗證碼數字。
String strrand=String.valueOf(random.nextInt(10));
//產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red=random.nextInt(200);
green=random.nextInt(200);
blue=random.nextInt(200);
//產生隨機高度 13至height之間
float imght = 0;
while(imght<=12){
imght = Float.parseFloat(String.valueOf(random.nextInt(height)));
}
//用隨機產生的顏色將驗證碼繪制到圖像中。
g.setColor(new Color(red,green,blue));
g.drawString(strrand,13*i+6,imght);
//將產生的四個隨機數組合在一起。
randomcode.append(strrand);
}
//將四位數字的驗證碼保存到session中。
HttpSession session=req.getSession();
session.setAttribute("VerifyCode",randomcode.toString());
//禁止圖像緩存。
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完畢,注意這句話session.setAttribute("VerifyCode",sRand);,我想是做JSP的應該都知道這個是什么意思吧,對了,這句話的意思就是把生成出來的數字保存到SESSION中去,我們在來配置下WEB.XML文件來讓這個SERVLET可以用: http://bbs.8isp.cn
http://bbs.8isp.cn
WEB.XML如下: http://bbs.8isp.cn
http://bbs.8isp.cn
<?xml version="1.0" encoding="UTF-8"?> http://bbs.8isp.cn
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " http://java.sun.com/dtd/web-app_2_3.dtd "> http://bbs.8isp.cn
<web-app> http://bbs.8isp.cn
<servlet> http://bbs.8isp.cn
<servlet-name>Code</servlet-name> http://bbs.8isp.cn
<servlet-class>dreamtime.dreamnews.ImageServlet</servlet-class> http://bbs.8isp.cn
</servlet> http://bbs.8isp.cn
<servlet-mapping> http://bbs.8isp.cn
<servlet-name>Code</servlet-name> http://bbs.8isp.cn
<url-pattern>/Code</url-pattern> http://bbs.8isp.cn
</servlet-mapping> http://bbs.8isp.cn
</web-app> http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
這樣這個SERVLET就可以用了... http://bbs.8isp.cn
http://bbs.8isp.cn
我們來看A.HTM怎么調用這個SERVLET來實現驗證碼: http://bbs.8isp.cn
http://bbs.8isp.cn
A.HTM如下: http://bbs.8isp.cn
http://bbs.8isp.cn
<FORM ACTION=B.JSP NAME=FORM > http://bbs.8isp.cn
用戶名:<input class=input maxlength=255 size=10 name=username> http://bbs.8isp.cn
密 碼:<input class=input type=password maxlength=255 size=10 name=password> http://bbs.8isp.cn
http驗證碼:<input name=image type=text value="請在這里輸入右邊的驗證碼" http://bbs.8isp.cn
size=7 maxlength=255> <img src="Code" onclick="history.go(0)" title="點擊刷新驗證碼"/> http://bbs.8isp.cn
</FORM> http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
A.HTM看起來很簡單,紅色的字是顯示SERVLET來讓驗證碼出現在網頁上~~~蘭色的字是個讓用戶輸入驗證碼的地方,其實就是個普通的INPUT, http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
http://bbs.8isp.cn
在來看看B.JSP是如何接收的.... http://bbs.8isp.cn
http://bbs.8isp.cn
B.JSP如下: http://bbs.8isp.cn
http://bbs.8isp.cn
String rand = (String)session.getAttribute("VerifyCode"); //提取放在SESSION的數字; http://bbs.8isp.cn
String input = request.getParameter("image"); //接收從A.HTM傳遞過來的值 http://bbs.8isp.cn
http://bbs.8isp.cn
if(!rand.equals(input)){ http://bbs.8isp.cn
out.println("驗證碼不對"); http://bbs.8isp.cn
http://bbs.8isp.cn
}else{ http://bbs.8isp.cn
http://bbs.8isp.cn
out.println("驗證碼正確") http://bbs.8isp.cn
http://bbs.8isp.cn
} http://bbs.8isp.cn
http://bbs.8isp.cn