Link Address: http://blog.sina.com.cn/s/blog_407a68fc010006qo.html
1, altogether need 2 commonly used Java files (Randomcode.java and Randomcodectrl.java): (A,) Randomcode.java is a common Java file, the contents are as follows: Import Java.awt.Color ;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.util.Random;
Import Javax.imageio.imageio;import javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession; public class Randomcode {
/**
* Randomly get a font
* Randomly random number @param
* @return Font returns a new font
*/
Private Font Getsfont (random random) {
return new Font ("Fixedsys", font.center_baseline,18);
}
/**
* Returns a random color
* @param int FC random number
* @param int BC random number
* Randomly random number @param
* @return Color Returns a new color
*/
Private Color getrandcolor (int fc,int bc,random Random) {
if (fc>255) fc=255;
if (bc>255) bc=255;
int R=fc+random.nextint (BC-FC-6);
int G=fc+random.nextint (BC-FC-4);
int B=fc+random.nextint (BC-FC-8);
return new Color (R,G,B);
}
/**
* Generate random number pictures
*/
public void Getrandcode (HttpServletRequest request,httpservletresponse response) throws exception{
System.setproperty ("Java.awt.headless", "true");
HttpSession session = Request.getsession ();
int width=80, height=22;//set picture size
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Graphics g = image.getgraphics ();
Random random = new random ();
G.fillrect (1, 1, width, height);//Set border
G.setfont (New Font ("Times New Roman", font.roman_baseline,18));
G.setcolor (Getrandcolor (111,133,random));
Generate Random lines
for (int i=0;i<11;i++) {
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (13);
int yl = Random.nextint (15);
G.drawline (X,y,x+xl,y+yl);
}
Generate Random Points
G.setcolor (Getrandcolor (130,150,random));
Generate 5 Random numbers
String srand= "";
for (int i=0;i<5;i++) {
G.setfont (Getsfont (random));
G.setcolor (New Color (Random.nextint (101), Random.nextint (111), Random.nextint (121)));
String rand=string.valueof (getrandomstring (Random.nextint (36)));
Srand+=rand;
G.translate (Random.nextint (3), Random.nextint (3));
g.DrawString (rand,13*i,16);
}
Session.removeattribute ("Rand");
Session.setattribute ("Rand", SRand);
G.dispose ();
Imageio.write (Image, "JPEG", Response.getoutputstream ());
}
Private String getrandomstring (int num) {
String randstring = "0123456788ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Return string.valueof (Randstring.charat (num));
}
} (b,) Randomcodectrl.java is a servlet Java file, the contents are as follows: Import Java.io.ioexception;import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Randomcodectrl extends HttpServlet {private static final long serialversionuid = 1L; protected void doget (Ht Tpservletrequest req,
HttpServletResponse resp) throws Servletexception, IOException {
Resp.setcontenttype ("Image/jpeg");
Resp.setheader ("Pragma", "No-cache");
Resp.setheader ("Cache-control", "No-cache");
Resp.setdateheader ("Expires", 0);
Randomcode rc = new Randomcode ();
try{
Rc.getrandcode (REQ,RESP);
}catch (Exception e) {
System.err.println (e);
}
} public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}} 2, front page call; Example: http://127.0.0.1:8080/Randomcodectrl "/> 3, verify that the input information and randomly generated pictures display the same content: The randomly generated images are placed in the session before the randomly generated images in the Randomcode.java code, so simply determine the value of getattribute ("Rand") in the session and the value of the verification code entered by the user page.
Java implementation of random verification code picture