標籤:
本例使用一個JavaBean,名為Image.java,包com.zempty.bean下;
三個JSP檔案,分別為image.jsp、login.jsp、check.jsp。其中login.jsp是登入頁,image.jsp用來調用JavaBean顯示圖片,check.jsp用來檢測輸入的驗證碼是否正確。
Image.java代碼如下:
package com.zempty.bean;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;public class Image { // 驗證碼圖片中可以出現的字元集,可根據需要修改 private char mapTable[] = { ‘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‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ }; /** * 功能:產生彩色驗證碼圖片 參數width為產生圖片的寬度,參數height為產生圖片的高度,參數os為頁面的輸出資料流 */ public String getCertPic(int width, int height, OutputStream os) { if (width <= 0) width = 60; if (height <= 0) height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 擷取圖形上下文 Graphics g = image.getGraphics(); // 設定背景色 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, width, height); // 畫邊框 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 取隨機產生的認證碼 String strEnsure = ""; for (int i = 0; i < 4; ++i) { strEnsure += mapTable[(int) (mapTable.length * Math.random())]; } // 將認證碼顯示到映像中,如果要產生更多位的驗證碼,增加drawString語句 g.setColor(Color.BLACK); g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18)); String str = strEnsure.substring(0, 1); g.drawString(str, 8, 17); str = strEnsure.substring(1, 2); g.drawString(str, 20, 15); str = strEnsure.substring(2, 3); g.drawString(str, 35, 18); str = strEnsure.substring(3, 4); g.drawString(str, 45, 15); // 隨機產生10個幹擾點 Random rand = new Random(); for (int i = 0; i < 10; i++) { int x = rand.nextInt(width); int y = rand.nextInt(height); g.drawOval(x, y, 1, 1); } // 釋放圖形上下文 g.dispose(); try { // 輸出映像到頁面 ImageIO.write(image, "JPEG", os); } catch (IOException e) { System.out.println(e.getMessage()); return ""; } return strEnsure; }}
image.jsp代碼如下:
<%--注意:contentType為image/jpeg--%>
<%@ page contentType="image/jpeg" pageEncoding="UTF-8"%><jsp:useBean id="image" class="com.zempty.bean.Image" scope="session" /><% String str = image.getCertPic(0, 0, response.getOutputStream()); //將驗證碼存入session session.setAttribute("certCode", str); out.clear(); //這句和下面那句要加上,否則會出現getOutputStream() has already been called for this response的錯誤。 out = pageContext.pushBody();%>
login.jsp代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>登入頁面</title></head><body> <form name="form1" action="check.jsp" method="post"> 使用者名稱:<input type="text" name="username" /> <br /> 密 碼:<input type="password" name="password" /> <br /> 驗證碼:<input type="text" name="certCode" /><img src="image.jsp" /> <br /> <input type="submit" value="確定" /> </form></body></html>
check.jsp代碼如下:
<%@ page pageEncoding="UTF-8"%><% String certCode = request.getParameter("certCode"); if (certCode.equals((String) session.getAttribute("certCode"))) { out.print("驗證碼輸入正確!"); } else { out.print("驗證碼輸入錯誤!"); }%>
示範介面如下:
JSP執行個體:彩色驗證碼