1. html
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> my JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
<SCRIPT type = "text/JavaScript">
Function getvalidcod ()
{
Document. getelementbyid ("imgcode"). src = "validatecodeservlet? Date = '+ new date ();";
}
</SCRIPT>
</Head>
<Body onload = "getvalidcod ();">
This is my JSP page.
<Br>
<Input name = "validatacode" type = "text"
Style = "width: 52px; Height: 21px;">
& Nbsp;
Onclick = "javascript: This. src = 'validatecodeservlet? Date = '+ new date ();"'
Style = "margin: 0px 2px-4px 0px; cursor: Pointer" Title = "can't see clearly, change one? "/>
</Body>
</Html>
2. Web. xml
<! -- Logon verification code -->
<Servlet>
<Servlet-Name> validatecodeservlet </servlet-Name>
<Servlet-class> valdatecode. validatecodeservlet </servlet-class>
<Init-param>
<Param-Name> width </param-Name>
<Param-value> 70 </param-value>
</Init-param>
<Init-param>
<Param-Name> height </param-Name>
<Param-value> 20 </param-value>
</Init-param>
<Init-param>
<Param-Name> codecount </param-Name>
<Param-value> 4 </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-Name> validatecodeservlet </servlet-Name>
<URL-pattern>/validatecodeservlet </url-pattern>
</Servlet-mapping>
3. serlvert
Package valdatecode;
Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics2d;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. util. Random;
Import javax. ImageIO. ImageIO;
Import javax. servlet. servletexception;
Import javax. servlet. servletoutputstream;
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 {
/**
* Serialversionuid: Todo (describe in one sentence what this variable represents)
*
*/
Private Static final long serialversionuid = 1l;
// Verify the Image Width
Private int width = 60;
// Verify the Image Height
Private int Height = 20;
// Number of characters in the verification code
Private int codecount = 4;
Private int x = 0;
// Font height
Private int fontheight;
Private int Codey;
Char [] codesequence = {'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 '};
Public validatecodeservlet (){
Super ();
}
Public void destroy (){
Super. Destroy ();
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
This. dopost (request, response );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Define the Image Buffer
Bufferedimage buffimg = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics2d G = buffimg. creategraphics ();
// Create a random number generator class
Random random = new random ();
// Fill the image in white
G. setcolor (color. White );
G. fillrect (0, 0, width, height );
// Create a font. The font size is determined based on the Image Height.
Font font = new font ("fixedsys", Font. Plain, fontheight );
// Set the font
G. setfont (font );
// Draw a border
G. setcolor (color. Black );
G. drawrect (0, 0, width-1, height-1 );
// Generates 160 random interference lines, making it difficult for other programs to detect the authentication codes in the image.
// G. setcolor (color. light_gray );
G. setcolor (color. Pink );
For (INT I = 0; I <160; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int X1 = random. nextint (12 );
Int Y1 = random. nextint (12 );
G. drawline (X, Y, x + X1, Y + Y1 );
}
// Randomcode is used to save the randomly generated verification code so that the user can perform verification after logon.
Stringbuffer randomcode = new stringbuffer ();
Int Red = 0, Green = 0, Blue = 0;
// Generate a random codecount Verification Code
For (INT I = 0; I <codecount; I ++ ){
// Obtain the randomly generated verification code number
String strrand = string. valueof (codesequence [random. nextint (36)]);
// Generate random color components to construct the color value, so that the color values of each output number are different.
Red = random. nextint (255 );
// Green = random. nextint (255 );
Green = random. nextint (125 );
Blue = random. nextint (255 );
// Use a random color to draw the verification code to the image.
G. setcolor (new color (red, green, blue ));
G. drawstring (strrand, (I + 1) * X, Codey );
// Combine the four random numbers.
Randomcode. append (strrand );
}
// Save the four-digit verification code to the session
Httpsession session = request. getsession ();
Session. setattribute ("validatacode", randomcode. tostring ());
// Disable image caching
Response. setheader ("paragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. setcontenttype ("image/JPEG ");
// Output the image to the servlet output stream
Servletoutputstream SOS = response. getoutputstream ();
ImageIO. Write (buffimg, "Jpeg", SOS );
SOS. Close ();
}
/**
* Initialize and verify image attributes
*/
Public void Init () throws servletexception {
// Obtain the initial information from web. xml
// Width
String strwidth = This. getinitparameter ("width ");
// Height
String strheight = This. getinitparameter ("height ");
// Number of characters
String strcodecount = This. getinitparameter ("codecount ");
// Convert the configuration information to a value
Try {
If (strwidth! = NULL & strwidth. Length ()! = 0 ){
Width = integer. parseint (strwidth );
}
If (strheight! = NULL & strheight. Length ()! = 0 ){
Height = integer. parseint (strheight );
}
If (strcodecount! = NULL & strcodecount. Length ()! = 0 ){
Codecount = integer. parseint (strcodecount );
}
} Catch (numberformatexception e ){
}
X = width/(codecount + 1 );
Fontheight = height-2;
Codey = height-4;
}
}