JSP do not brush new verification code:
There are two pages in total:
First image. jsp
Image. jsp source code
[Java]
<% @ Page language = "java" import = "java. util. *, java. awt. image. *, java. awt. *, javax. imageio. * "pageEncoding =" GBK "contentType =" image/jpeg "%>
<! -- Remember contentType = "image/jpeg" -->
<%!
Color getRandColor (int start, int end, long seed)
{
If (Starting> 255)
{
Started = 255;
}
Random random = new Random (seed );
Int r = start + random. nextInt (end + 1-start );
Int g = start + random. nextInt (end + 1-start );
Int B = start + random. nextInt (end + 1-start );
Return new Color (r, g, B );
}
%>
<%
Int width = 60;
Int height = 20;
// Random letters in the generated verification code. You can set more
String [] str = {"1", "a", "A", "B", "c", "2", "I", "8 ", "U", "y "};
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Even if there is paper, you need to know the width and height.
Graphics g = image. getGraphics ();
// Even if there is a pen
G. setColor (getRandColor (230,250, System. currentTimeMillis ()));
// Even if the pen is dipped in color
G. fillRect (0, 0, width, height );
// Fill the background color
Random random = new Random (System. currentTimeMillis ());
// Draw interference lines
G. setColor (getRandColor (160,200, System. currentTimeMillis ()));
For (int I = 0; I <50; I ++)
{
G. drawLine (random. nextInt (6), random. nextInt (15 ),
Random. nextInt (6) + random. nextInt (56), random. nextInt (7) + random. nextInt (17 ));
}
String check = new String ();
G. setFont (new Font ("Times new Roman", Font. PLAIN, 18 ));
For (int I = 0; I <4; I ++)
{
G. setColor (getRandColor (20,110, System. currentTimeMillis () + I * 1000 ));
// String ran = String. valueOf (random. nextInt (10 ));
Int temp = random. nextInt (10 );
String ran = str [temp];
Check + = ran;
G. drawString (ran, 6 + I * 13, 16); // write numbers respectively.
}
// You can use the session to obtain the randomly generated verification code.
Session. setAttribute ("check", check );
G. dispose ();
// Make the image take effect
// Response. setCharacterEncoding ("GBK ");
// Request. setCharacterEncoding ("GBK ");
ImageIO. write (image, "JPEG", response. getOutputStream ());
// Output the image. The three parameters are paper, format, and output stream of the server.
Out. clear ();
Out = pageContext. pushBody ();
// With these two statements added, tomcat will have no exception information
%>
And use it elsewhere.
[Html]
<Body>
<a href = "#" onclick = "reload ()"> refresh </a>
<Script>
Function reload (){
Document. getElementById ("code"). setAttribute ("src", "image. jsp? A = "+ new Date (). getTime ());
}
</Script>
</Body>