Generate the servlet of the Verification Code image on the user login page

Source: Internet
Author: User

 

Code

 /*
* To change this template, choose tools | templates
* And open the template in the editor.
*/

Package com. Qixin. Web;

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;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

/**
*
* @ Author Qixin
*/
Public class imageexample extends httpservlet {
// Used to generate a random number
Private random rand;
// Font used to display numbers
Private Font font;
// Width and height of the image
Private int width;
Private int height;

/**
* The input parameter is the output stream. This method generates an image and writes the data to the output stream.
* @ Param out
* @ Throws ioexception
*/
Private void outputimage (outputstream out) throws ioexception {
// Generate a random number of 1000-9999 and convert it into a string
String STR = integer. tostring (1000 + Rand. nextint (9000 ));
// Create a specified width and height image in the memory
Bufferedimage Bi = new bufferedimage (width, height, bufferedimage. type_int_rgb );
// Enter 20 white pixels at random
For (INT I = 0; I <20; I ++ ){
Int x = Rand. nextint (width );
Int y = Rand. nextint (height );
Bi. setrgb (X, Y, 0 xffffff );
}
// Obtain the object used to draw a character image
Graphics G = Bi. getgraphics ();
G. setfont (font );
// Draw characters. Note that the last two Parameters specify the coordinates in the lower left corner of the first character.
G. drawstring (STR, 0, height-1 );
// Resources are released immediately after graphics is used up to avoid waiting for garbage collection. Reduces resource usage
G. Dispose ();
// Send the image data to the output stream in JPG format. This method may throw an ioexception.
ImageIO. Write (Bi, "jpg", out );
}

@ Override
Public void Init (){
Rand = new random (system. currenttimemillis ());
Font = new font ("Courier New", Font. italic, 20 );
Width = 50;
Height = 20;
}
/**
* Processes requests for both HTTP <code> Get </code> and <code> post </code> methods.
* @ Param request Servlet Request
* @ Param response servlet response
* @ Throws servletexception if a servlet-specific error occurs
* @ Throws ioexception if an I/O error occurs
*/
Protected void processrequest (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Set the MIME type to JPEG Image
Response. setcontenttype ("image/JPEG ");
// Obtain the binary output stream and pass it to the method for generating the image to return data
Outputimage (response. getoutputstream ());
}

// <Editor-fold defaultstate = "Collapsed" DESC = "httpservlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code> Get </code> method.
* @ Param request Servlet Request
* @ Param response servlet response
* @ Throws servletexception if a servlet-specific error occurs
* @ Throws ioexception if an I/O error occurs
*/
@ Override
Protected void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Processrequest (request, response );
}

/**
* Handles the HTTP <code> post </code> method.
* @ Param request Servlet Request
* @ Param response servlet response
* @ Throws servletexception if a servlet-specific error occurs
* @ Throws ioexception if an I/O error occurs
*/
@ Override
Protected void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Processrequest (request, response );
}

/**
* Returns a short description of the servlet.
* @ Return a string containing servlet description
*/
@ Override
Public String getservletinfo (){
Return "Short Description ";
} // </Editor-fold>

}

 

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.