Image_height
"/" font-family = / "arial /" font-size = / ""
Image_Height "/" Fill = / "Black /"> ";
Private static final string svg_source2 = " text> svg>";
/ *
* If Graphic Environment Not Avaliable (EG: Run Under Linux Console), USE
* SVG INSTEAD OF JPEG.
* /
Private boolean svgmode = false;
Private printwriter out = null;
/ *
* (non-javadoc)
*
* @see javax.servlet.http.httpservlet # service (javax.servlet.http.httpservletRequest, * javax.servlet.http.httpservletResponse)
* /
Protected void service (httpservletRequest arg0, httpservletresponse arg1)
Throws ServleTexception, IOException
{
IF (SN == Null)
Return;
String vcode = sn2vcode ();
Arg0.GetSession (). SetAttribute ("SRAND", Vcode); // Code content writes in session
IF (svgmode)
Outsvg (vcode, arg1);
Else
Outjpeg (Vcode, Arg1);
}
Private Void Outsvg (String Vcode, HTTPSERVLETRESPONSE RESP)
Throws oException
{
Resp.setContentType ("Image / SVG XML");
Resp.getOutputStream (). Print (svg_source1 vcode svg_source2);
}
Private void Outjpeg (String Vcode, HttpservletResponse Resp)
Throws oException
{
Resp.setContentType ("Image / JPEG");
BufferedImage Image = New BufferedImage (image_width, image_height,
BufferedImage.Type_INT_RGB);
Random Random = new random ();
Graphics g = image.getgraphics ();
G.SetColor (GetrandColor (200, 250));
G.FillRect (0, 0, image_width, image_height);
G.SetFont (New Font ("Times New Roman", Font.hanging_Baseline, 18);
G.SetColor (GetrandColor (160, 200));
For (int i = 0; i <155; i )
{
INT x = random.nextint (image_width);
INT Y = random.nextint (image_height);
INT XL = random.nextint (12);
INT YL = random.nextint (12);
g.drawline (X, Y, X XL, Y YL);
}
For (INT i = 1; i <= 4; i )
{
String rand = vcode.substring (i - 1, i);
G.SetColor (New Color (20 Random.Nextint (110), 20 Random
.nextint (110), 20 random.nextint (110)));
g.drawstring (rand, 13 * (i - 1) 0, 16);
}
Imagewriter Writer = (ImageWriter) Imageio.GetImageWritersbyFormatName ("JPEG"). Next ();
JPEGIMAGEWRITEPARAM params = new JPEGIMAGEWRITEPARAM (NULL);
ImageOutputStream iOS = Imageio.createImageOutputStream (respon
.getOutputStream ());
Writer.setput (iOS);
Writer.write (NULL, New IIOIMAGE (Image, Null, Null), Params
Writer.dispose ();
iOS.Close ();
}
Static string Sn2vcode ()
{
String SRAND = "";
Random Random = new random ();
For (int i = 0; i <4; i )
{
String Rand = String.Valueof (random.nextint (10));
SRAND = Rand;
}
Return SRAND;
}
/ *
* (non-javadoc)
*
* @see javax.servlet.GenericServlet # init ()
* /
Public void init () THROWS servletexception
{
Try
{
Graphicsenvironment.getlocalgraphicsenvironment ();
}
Catch (throwable e)
{
// Run Under Linux Console???
svgmode = true;
}
}
/ **
* Get random colors
* @Param FC
* @Param BC
* @Return
* /
Public 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);
}
}
Add to Web.xml
VerifyCodeServlet servlet-name> cmcc.verifycodeServlet servlet-class> servlet> verifycodeservlet servlet-name> / verifycodeservlet / * url-pattern> servlet-maping> Call this servlet with http: // hostname / verifycodeServlet / you can! Call