Import java. Io .*;
Import java. util .*;
Import com.sun.image.codec.jpeg .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. AWT .*;
Import java. AWT. image .*;
/**
* Title: getimg. Java
* Description: This class generates a 4-digit verification code at random and writes it to the session,
*/
Public class getimg extends httpservlet {
Private font mfont = new font ("", Font. Plain, 12); // set the font
// Process post
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
// Obtain a random number of 1000-9999
String S = "";
Int intcount = 0;
Intcount = (new random (). nextint (9999 );//
If (intcount <1000) intcount + = 1000;
S = intcount + "";
// Pay for the session.
Httpsession session = request. getsession (true );
Session. setattribute ("getimg", S );
Response. setcontenttype ("image/GIF ");
Servletoutputstream out = response. getoutputstream ();
Bufferedimage image = new bufferedimage (35,14, bufferedimage. type_int_rgb );
Graphics gra = image. getgraphics ();
// Set the background color
Gra. setcolor (color. Yellow );
Gra. fillrect );
// Set the font color
Gra. setcolor (color. Black );
Gra. setfont (mfont );
// Output number
Char C;
For (INT I = 0; I <4; I ++ ){
C = S. charat (I );
Gra. drawstring (C + "", I * 7 + 4, 11); // 7 indicates the width, and 11 indicates the upper and lower heights.
}
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (image );
Out. Close ();
}
}