如今網絡上各個網站上都有驗證碼,可是實現的形形×××各不相同.經過查資料和摸索,使用servlet來實現驗證碼.
一 建一個驗證碼生成類RandomImageGenerator ,其功能就是實現驗證碼
該類實現以下
import java.awt.*;
import java.awt.p_w_picpath.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.p_w_picpathio.ImageIO;
import org.apache.commons.lang.RandomStringUtils;
/**
* 隨即圖片生成器
* 該類用於用戶註冊時候須要用戶根據圖片內容進行填寫正確後方可註冊
* @author Liudong
*/
public class RandomImageGenerator {
//隨即生成包含驗證碼的字符串
public static String random() {
//20060320 add by wyx
//由於o和0,l和1很難區分,因此,去掉大小寫的o和l
String str = "";
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";//初始化種子
return RandomStringUtils.random(6, str);//返回6爲的字符串
}
/**
* 根據要求的數字生成圖片,背景爲白色,字體大小16,字體顏色黑色粗體
* @param num 要生成的數字
* @param out 輸出流
* @throws IOException
*/
public static void render(String num, OutputStream out) throws IOException {
if (num.getBytes().length > 6) {
throw new IllegalArgumentException(
"The length of param num cannot exceed 6.");
}
//設定寬度和高度
int width = 130;
int height = 30;
// 在內存中建立圖象
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics2D g = (Graphics2D) bi.getGraphics();
//畫邊框
java.util.Random random = new java.util.Random();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//設置字體
Font mFont = new Font("Tahoma", Font.BOLD, 16);
g.setFont(mFont);
g.setColor(Color.BLACK);//設置字體顏色
//畫認證碼,每一個認證碼在不一樣的水平位置
String str1[] = new String[6];
for (int i = 0; i < str1.length; i++) {
str1[i] = num.substring(i, i + 1);
int w = 0;
int x = (i + 1) % 3;
//隨即生成驗證碼字符的水平偏移量
if (x == random.nextInt(3)) {
w = 19 - random.nextInt(7);
} else {
w = 19 + random.nextInt(7);
}
//隨即生成顏色
Color color1 = new Color(random.nextInt(180), random.nextInt(180),
random.nextInt(180));
g.setColor(color1);
g.drawString(str1[i], 20 * i + 10, w);
}
// 隨機產生干擾點,並用不一樣的顏色表示,使圖象中的認證碼不易被其它程序探測到
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //隨即畫各類顏色的點
g.drawOval(x, y, 0, 0);
}
//畫干擾線
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //隨即畫各類顏色的線
g.drawLine(x, y, x1, y1);
}
//圖像生效
g.dispose();
//輸出頁面
ImageIO.write(bi, "jpg", out);
}
public static void main(String[] args) throws IOException {
String num = random();
System.out.println(num);
render(num, new FileOutputStream("D:\\test.jpg"));
System.out.println("Image generated.");
}
}
二 驗證碼的實現,使用servlet來實現驗證碼
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import peopleBlog.util.RandomImageGenerator;
/**
* 用於產生隨即圖片以防止非法***
* @author Liudong
*/
public class RandomImageServlet extends HttpServlet {
public final static String RANDOM_LOGIN_KEY = "RANDOM_LOGIN_KEY";
public void init() throws ServletException {
System.setProperty("java.awt.headless","true");
}
public static String getRandomLoginKey(HttpServletRequest req) {
return (String)req.getSession().getAttribute(RANDOM_LOGIN_KEY);
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
HttpSession ssn = req.getSession();
if(ssn!=null) {
String randomString = RandomImageGenerator.random();//生成種子
ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);//將種子放到session裏面
res.setContentType("p_w_picpath/jpeg");//設置圖像生成格式
RandomImageGenerator.render(randomString,res.getOutputStream());//輸出到頁面
}
}
}
其中 ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);這一行代碼的做用是:
當從頁面上輸入驗證碼,提交後,在後臺sevlet或者action裏面驗證輸入的驗證碼和session裏面的是否一致,若是不一致返回錯誤信息.
三,頁面實現
在web.xml文件中設置
<servlet>
<servlet-name>p_w_picpath</servlet-name>
<servlet-class>peopleBlog.RandomImageServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>p_w_picpath</servlet-name>
<url-pattern>/verifyCode.jsp</url-pattern>
</servlet-mapping>
而後在jsp頁面中
<td><input name="verifyCode" type="text" id="verifyCode" class="input2" style="width:55px;" onkeydown="JavaScript:ifEnter()"/> <img src="/blog/verifyCode.jsp" align="absMiddle" border="0"/> </td>
再在目的servlet中進行驗證碼判斷就ok了