JSP generates a Random verification code and refreshes the verification code locally.

Source: Internet
Author: User

1. Foreground jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<%

String path = request. getContextPath ();

String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";

%>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

<Html>

<Head>

<Base href = "<% = basePath %>">

<Title> Administrator Logon </title>

<Script type = "text/javascript" language = "javascript">

Function change (){

Var o = document. getElementById ("random ");

Var timenow = new Date (). getTime ();

O. src = "<% = path %>/server/ImageValidate? D = "+ timenow;

}

</Script>

</Head>

<Body>

/server/ImageValidate"/>

<Input type = "button" value = "change image" id = "label" onclick = "change ()"> </input>

</Body>

</Html>

 

2. servlet with random code in the background

Import java. AWT. color;

Import java. AWT. Font;

Import java. AWT. graphics;

Import java. AWT. image. bufferedimage;

Import java. Io. ioexception;

Import java. util. Random;

Import javax. servlet. servletexception;

Import javax. servlet. servletoutputstream;

Import javax. servlet. http. httpservlet;

Import javax. servlet. http. httpservletrequest;

Import javax. servlet. http. httpservletresponse;

 

Import com.sun.image.codec.jpeg. Unzip codec;

Import com.sun.image.codec.jpeg. encode imageencoder;

 

@ Suppresswarnings ("serial ")

Public class imagevalidate extends httpservlet {

Public imagevalidate (){

Super ();

}

Public void destroy (){

Super. destroy (); // Just puts "destroy" string in log

}

Public void doGet (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

Request. setCharacterEncoding ("GBK ");

Response. setCharacterEncoding ("GBK ");

Response. setContentType ("image/jpeg ");

Response. addHeader ("pragma", "NO-cache ");

Response. addHeader ("Cache-Control", "no-cache ");

Response. addDateHeader ("Expries", 0 );

Int width = 80, height = 30;

Response. setContentType ("text/html; charset = GBK ");

String chose = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

Char temp; // Save the randomly generated chose Element

Random rand = new Random ();

Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

Graphics G = image. getgraphics ();

// Fill in the background color below

G. setcolor (color. Yellow );

G. fillrect (0, 0, width, height );

// Draw a border

G. setcolor (color. Black );

Font font;

G. drawrect (0, 0, width-1, height-1 );

Stringbuffer srand = new stringbuffer ();

For (INT I = 0; I <4; I ++ ){

Temp = chose. charat (RAND. nextint (chose. Length ()));

Srand. append (temp );

Font = new font ("Arial", Font. Plain, 15 + Rand. nextint (10); // font

G. setFont (font );

G. setColor (new Color (rand. nextInt (255), 0, rand. nextInt (255); // verification code Color

G. drawString (String. valueOf (temp), 15 * I + 5, 20 + rand. nextInt (10); // location

}

Request. getSession (). setAttribute ("randomNumber", sRand. toString (); // Save the generated verification code value

// Set the font color

G. dispose ();

ServletOutputStream outStream = response. getOutputStream ();

Required imageencoder encoder = required codec. createJPEGEncoder (outStream );

Encoder. encode (image );

OutStream. close ();

}

Public void init () throws ServletException {

}

}

3. xml configuration file <servlet> <servlet-name> ImageValidate </servlet-name> <servlet-class> user. imageValidate </servlet-class> </servlet> <servlet-mapping> <servlet-name> ImageValidate </servlet-name> <url-pattern>/server/ImageValidate </url -pattern> </servlet-mapping>
Related Article

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.