A. JSP page
<script language= "javascript" type= "Text/javascript" >
Refresh Verification Code
function Showrand () {
document.getElementById ("random"). src = "<%=path%>/common/image.jsp?date=" + new Date ();
}
</script>
<body>
<p class= "test" id= "P_randcode" style= "Display:none" >
<b> Verification Code </b>
<input type= "text" value= "" class= "Txt_change" id= "Confirmcode" name= "Confirmcode" >
<a style= "Cursor:pointer;align:left" onclick= "Showrand ();" > Change one </a>
<input type= "hidden" id= "Confirmrand" name= "Confirmrand"/>
</p>
</body>
Two. Display the image.jsp of the Verification code picture
<%@ page contenttype= "image/jpeg" import= "com.sun.image.codec.jpeg.*,java.awt.image.*,java.awt.*,java.util.*"% >
<%
Request.setcharacterencoding ("GBK");
Response.setcontenttype ("text/html; CHARSET=GBK ");
String rand = "0000";//randomly generated verification code
if (Request.getparameter ("rand") = null) {
Rand = (String) request.getparameter ("Rand");
Request.getsession (). SetAttribute ("Rand", Rand);
}
try{
Out.clear ();
Response.setcontenttype ("Image/jpeg");
Response.AddHeader ("Pragma", "no-cache");
Response.AddHeader ("Cache-control", "No-cache");
Response.adddateheader ("Expries", 0);
int width =, height = 20;
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics g = image.getgraphics ();
Random random = new random ();
int fc = $, BC = 255;
if (FC > 255) {
FC = 255;
}
if (BC > 255) {
BC = 255;
}
int red = FC + Random.nextint (BC-FC);
int green = FC + Random.nextint (BC-FC);
int blue = FC + Random.nextint (BC-FC);
Color color = new color (red, green, blue);
G.setcolor (color);
G.setfont (New Font ("Times New Roman", Font.plain, 18));
G.fillrect (0, 0, width, height);
for (int i = 0; i <; i++) {
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.drawline (x, y, X + xl, y + yl);
}
int stringlength = 4;
String srand= "";
for (int i = 0; i < stringlength; i++) {
String randstr = string.valueof (Rand.charat (i));
SRAND+=RANDSTR;
G.setcolor (New Color (+ random.nextint), + random
. Nextint (+), + random.nextint (110));
g.DrawString (RANDSTR, * i + 6, 16);
}
Session.setattribute ("Ccode", SRand);
G.dispose ();
Servletoutputstream OutStream = Response.getoutputstream ();
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (OutStream);
Encoder.encode (image);
Outstream.close ();
Out.clear ();
out = Pagecontext.pushbody ();
} catch (Exception e) {
}
%>
JSP Verification Code