Modify the src attribute of an image and assign a value to it to refresh the image,
However, due to the cache issue, if both values are the same, the browser generally does not refresh the image. Therefore, you need to make no changes to the image connection each time.
Whether a is obtained from the verifycodeservlet page or not, it is only used for Refresh.
Package internetmanagerservlet;
Import java. Io .*;
Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. graphicsenvironment;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. util. Random;
Import javax. ImageIO. iioimage;
Import javax. ImageIO. ImageIO;
Import javax. ImageIO. imagewriter;
Import javax.imageio.plugins.jpeg. Export imagewriteparam;
Import javax. ImageIO. Stream. imageoutputstream;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class verifycodeservlet extends httpservlet
{
Private Static final int image_width = 60;
Private Static final int image_height = 20;
Private Static final int font_height = 16;
Private Static final string svg_source1 = "<! Doctype SVG public/"-// W3C // dtd svg 1.0 // en/"/"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd/">"
+ "<SVG width = /""
+ Image_width
+ "/" Height = /""
+ Image_height
+ "/" Xmlns =/"http://www.w3.org/2000/svg/">"
+ "<Text x =/" 0/"Y = /""
+ Image_height
+ "/" Font-family =/"Arial/" font-size = /""
+ Image_height + "/" fill =/"Black/"> ";
Private Static final string svg_source2 = "</text> </SVG> ";
Private Boolean svgmode = false;
Private printwriter out = NULL;
Protected void Service (httpservletrequest arg0, httpservletresponse arg1) throws servletexception, ioexception
{
// If (Sn = NULL)
// Return;
String vcode = sn2vcode ();
Arg0.getsession (). setattribute ("RAND", vcode); // write the code content to the session.
If (svgmode)
Outsvg (vcode, arg1 );
Else
Outjpeg (vcode, arg1 );
}
Private void outsvg (string vcode, httpservletresponse resp) throws ioexception
{
Resp. setcontenttype ("image/SVG + XML ");
Resp. getoutputstream (). Print (svg_source1 + vcode + svg_source2 );
}
Private void outjpeg (string vcode, httpservletresponse resp) throws ioexception
{
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. Plain, 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 ();
Required imagewriteparam Params = new required imagewriteparam (null );
Imageoutputstream IOS = ImageIO. createimageoutputstream (resp. getoutputstream ());
Writer. setoutput (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;
}
Public void Init () throws servletexception
{
Try
{
Graphicsenvironment. getlocalgraphicsenvironment ();
}
Catch (throwable E)
{
Svgmode = true;
}
}
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 );
}
}
Web. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet-Name> verifycodeservlet </servlet-Name>
<Servlet-class> internetmanagerservlet. verifycodeservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> verifycodeservlet </servlet-Name>
<URL-pattern>/verifycodeservlet </url-pattern>
</Servlet-mapping>
</Web-app>