JSP instance: Color Verification Code

Source: Internet
Author: User

This example uses a JavaBean, named Image.java, under the package com.zempty.bean;

Three JSP files, respectively image.jsp, login.jsp, check.jsp. Where login.jsp is the login page, image.jsp is used to invoke the JavaBean display picture, check.jsp is used to detect the correctness of the input verification code.

The Image.java code is as follows:

 PackageCom.zempty.bean;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.util.Random;ImportJavax.imageio.ImageIO; Public classImage {//a character set that can appear in a captcha picture and can be modified as needed    Private CharMaptable[] = {' 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 ' }; /*** Function: Generate color verification Code picture parameter width for the resulting picture, the height of the parameter is generated, the parameter OS is the output stream of the page*/     PublicString Getcertpic (intWidthintheight, outputstream os) {        if(Width <= 0) Width= 60; if(Height <= 0) Height= 20; BufferedImage Image=Newbufferedimage (width, height, bufferedimage.type_int_rgb); //Get the graphics contextGraphics g =Image.getgraphics (); //Set Background colorG.setcolor (NewColor (0xDCDCDC)); G.fillrect (0, 0, width, height); //Draw BorderG.setcolor (Color.Black); G.drawrect (0, 0, width-1, height-1); //take the randomly generated authentication codeString strensure = "";  for(inti = 0; I < 4; ++i) {strensure+ = maptable[(int) (Maptable.length *Math.random ())]; }        //Display the authentication code in the image, if you want to generate more bits of the verification code, add drawstring statementG.setcolor (Color.Black); G.setfont (NewFont ("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); //randomly generates 10 interference pointsRandom Rand =NewRandom ();  for(inti = 0; I < 10; i++) {            intx =rand.nextint (width); inty =rand.nextint (height); G.drawoval (x, Y,1, 1); }        //releasing the graphics contextG.dispose (); Try {            //output image to pageImageio.write (Image, "JPEG", OS); } Catch(IOException e) {System.out.println (E.getmessage ()); return""; }        returnstrensure; }}

The image.jsp code is as follows:

<%--Note: contenttype is image/jpeg--%>
class= "Com.zempty.bean.Image" scope= "session"/><% = image.getcertpic (0, 0, Response.getoutputstream ()); // Save verification Code to session Session.setattribute ("Certcode", str); Out.clear (); This sentence and the following sentence to add, otherwise there will be Getoutputstream () has already been called for this response error. = pagecontext.pushbody (); %>

The login.jsp code is as follows:

<%@ 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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Login Page</title></Head><Body>    <formname= "Form1"Action= "check.jsp"Method= "POST">User name:<inputtype= "text"name= "username" /> <BR/>Dense&nbsp;&nbsp;Code:<inputtype= "Password"name= "Password" /> <BR/>Verification Code:<inputtype= "text"name= "Certcode" /><imgsrc= "image.jsp" /> <BR/> <inputtype= "Submit"value= "OK" />    </form></Body></HTML>

The check.jsp code is as follows:

<%@ page pageencoding= "UTF-8"%><%    = Request.getparameter ("Certcode");     if (Certcode.equals (String) session.getattribute ("Certcode")){        Out.print ("Verification code entered correctly! ");     Else {        Out.print ("Verification code input Error! ");    } %>

The demo interface is as follows:

JSP instance: Color Verification Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.