JSP verification code page notes

Source: Internet
Author: User

First, I searched the code for JSP verification code generation on the Internet, as shown below:

Package com. servlet;

Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import java. util. arrays;
Import java. util. collections;
Import java. util. List;
Import java. util. Random;

Import javax. ImageIO. ImageIO;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;

Public class validatecodeservlet extends httpservlet {

/**
*
*/
Private Static final long serialversionuid = 4008416931787800531l;

/**
* Constructor of the object.
*/
Public validatecodeservlet (){
Super ();
}

/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. Destroy (); // just puts "Destroy" string in log
}

/**
* The doget method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to get.
*
* @ Param request
* The request send by the client to the server
* @ Param response
* The response send by the server to the client
* @ Throws servletexception
* If an error occurred
* @ Throws ioexception
* If an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

// Set the page not to cache
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );

// Create an image in memory
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );

// Obtain the painting object
Graphics G = image. getgraphics ();
// Set the background color
G. setcolor (getrandcolor (225,250 ));
G. fillrect (0, 0, width, height );
// Set the font
G. setfont (new font ("Times New Roman", Font. Bold, 20 ));

// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
Random random = new random ();
G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int XL = random. nextint (12 );
Int yl = random. nextint (12 );
G. drawline (X, Y, x + XL, Y + yl );
}

// Obtain the random ID code (4 digits)
String srand = generateword ();
Byte [] cs = srand. getbytes ();
For (INT I = 0; I <4; I ++ ){
String A = new string (CS, I, 1 );
// String Rand = string. valueof (random. nextint (10 ));
String Rand =;
// Srand + = rand;
// Display the authentication code to the image
G. setcolor (new color (20 + random. nextint (110), 20 + random
. Nextint (110), 20 + random. nextint (110); // the color of the called function is the same. It may be because the seed is too close, so it can only be generated directly.
G. drawstring (RAND, 13 * I + 6, 16 );
}

// Save the authentication code to the session
Httpsession session = request. getsession ();
Session. setattribute ("valicode", srand );

// The image takes effect
G. Dispose ();

// Output the image to the page
ImageIO. Write (image, "Jpeg", response. getoutputstream ());

}

/**
* The dopost method of the servlet. <br>
*
* This method is called when a form has its tag Value Method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws servletexception if an error occurred
* @ Throws ioexception if an error occurred
*/
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}

/**
* Obtain random colors in a given range
* @ Param FC
* @ Param BC
* @ Return
*/
Private color getrandcolor (int fc, int BC ){
Random random = new random ();
If (FC> 255)
Fc = 255;
If (BC> 255)
BC = 255;
Int r = FC + random. nextint (BC-Fc );
Int G = FC + random. nextint (BC-Fc );
Int B = FC + random. nextint (BC-Fc );
Return new color (R, G, B );
}
Private string generateword (){
String [] beforeshuffle = new string [] {"1", "2", "3", "4", "5", "6", "7 ",
"8", "9", "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 "};
List list = arrays. aslist (beforeshuffle );
Collections. Shuffle (list );
Stringbuilder sb = new stringbuilder ();
For (INT I = 0; I <list. Size (); I ++ ){
SB. append (list. Get (I ));
}
String aftershuffle = sb. tostring ();
String result = aftershuffle. substring (5, 9 );
Return result;
}
}

I processed it myself and used to generate random numbers. Here I use a list set.

Then reference this servlet on the JSP page, as shown below:

/validatecodeservlet" Flush = "true" onclick = "reloadimage ()" style = "vertical-align: bottom; cursor: Pointer "> </img>

Note: cursor: pointer is a hand-changing method, and reloadimage () is a js method.

Function reloadimage (){
Document. getelementbyid ('servletimg '). src = "<% = PATH %>/validatecodeservlet ";
}

Set the same address again.

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.