The Demo code consists of the following three parts:
1. checkCode. java: used to generate a verification code
2. checkCodeServler
3. check. jsp Verification
The content of checkCode. java is as follows:
Copy codeThe Code is as follows:
// Obtain a four-digit random number
Private char mapTable [] = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 '};
// Generate the verification code and return the randomly generated number
Public String getEnsure (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 );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Set the background color
G. setColor (new Color (0 xDCCCCC ));
G. fillRect (0, 0, width, height );
// Draw a border
G. setColor (Color. black );
G. drawRect (0, 0, width-1, height-1 );
// Obtain the random ID
String strEnsure = "";
// 4 indicates the 4-digit Verification Code
For (int I = 0; I <4; ++ I ){
StrEnsure + = mapTable [(int) (mapTable. length * Math. random ()];
}
// Display the authentication code to the image
G. setColor (Color. red );
G. setFont (new Font ("Atlantic Inline", Font. PLAIN, 14 ));
// Coordinates of the image
String str = strEnsure. substring (0, 1 );
G. drawString (str, 8, 14 );
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 );
// Release the image Context
G. dispose ();
Try {
// Output the image to the page
ImageIO. write (image, "JPEG", OS );
} Catch (IOException e ){
Return "";
}
Return strEnsure; // return the generated random number.
}
The content of checkCodeServlet
Copy codeThe Code is as follows:
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Disable the cache. Every time you access this page, the cache is generated again.
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// The instance object that generates the verification code
CheckCode ie = new CheckCode ();
// Call the method and return the string in the generated Verification Code
String str = ie. getEnsure (0, 0, response. getOutputStream ());
// Obtain the session and save the string in the session to lay the foundation for subsequent comparison.
HttpSession session = request. getSession ();
Session. setAttribute ("strEnsure", str );
}
Then configure servlet in web. xml
Copy codeThe Code is as follows:
<Servlet>
<Servlet-name> CheckServlet </servlet-name>
<Servlet-class> com. blog. servlet. CheckServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> CheckServlet </servlet-name>
<Url-pattern>/check </url-pattern>
</Servlet-mapping>
Finally, the reference of the jsp page
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> Verification Code </title>
<Script type = "text/javascript" language = "javascript">
// Re-obtain the verification character
Function changeImage ()
{
// Click the trigger image reload event to complete the replacement of the image Verification Code
Document. getElementById ("imgRandom"). src = document. getElementById ("imgRandom"). src + '? ';
}
</Script>
</Head>
<Body>
<A href = "javascript: changeImage ();"> Why Not? </A>
</Body>
</Html>
On the jsp page, you only need to point the src attribute of img to the servlet that generates the verification code, and point to the url mapped by servle in web. xmlt.