JSP執行個體:彩色驗證碼

來源:互聯網
上載者:User

標籤:

本例使用一個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 /> 密&nbsp;&nbsp;碼:<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執行個體:彩色驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.