Java code: Image stream generation
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
String vcode = drawgraphic (image );
Vcode = encrypt (vcode );
Cookie = new cookie (randomcodekey, vcode );
Cookie. setmaxage (-1 );
Cookie. setpath ("/");
Response. addcookie (cookie );
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. setcontenttype ("image/JPEG"); // This sentence is required and cannot be missing
Servletoutputstream SOS = NULL;
Try {
SOS = response. getoutputstream ();
ImageIO. Write (image, "Jpeg", SOS );
} Catch (exception e ){
Throw new runtimeexception (E );
}
Finally {
Try {SOS. Close ();} catch (ioexception e) {e. printstacktrace ();}
}
The logic of the Action written in JS cannot be missing because different browsers have different requests for the action:
Function changeimg (){
$ ("# Image"). ATTR ("src", '/IMG? S = '+ math. Random ());
}
HTML code:
<A href = "#" onclick = "changeimg ();"> change one </a>