Last night in Csdn saw a predecessor write a ajax+servlet+jsp verification. Suddenly whim, in the reading of the code of the predecessor I personally experience, do a verification code generation tool class. For everyone to do a reference.
1: join the veriycodeutils class to generate the CAPTCHA image
Package Com.servlet;import Java.awt.color;import Java.awt.font;import java.awt.graphics2d;import Java.awt.image.bufferedimage;import Java.io.outputstream;import Java.util.random;import javax.imageio.ImageIO;/** * * @author Hubiao * CAPTCHA Generator * Use the API * BufferedImage to create an image * graphics2d draw * FillRect (x,y,width,height); background * font () fonts * d Rawrect (); border * DrawLine (); line * drwastring: Image data * ImageIO Generate image */public class Veriycodeutils {/** * @param output Save stream of validated images * @return Verification Code */public static String newveriycode (OutputStream output) {int width = 90;int height = 40;int Codecount = 5;ch Ar[] codesequence = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '}; Create Image object, 8-bit rgbbufferedimage buffered = new BufferedImage (width, height, bufferedimage.type_int_rgb);// By crapahices to draw the image into BufferedImage graphics2d gra = Buffered.creategraphics ();//Set Picture background:White Gra.setcolor (Color.White); gra.fillrect (0, 0, width, height);//set font, font size is determined by the height of the image gra.setfont (new font ("Fixedsys", font.plain,height-2));//Set border: Black, 1cmgra.setcolor (Color.Black); gra.drawrect (0, 0, width-1, height-1);// Generate 10 black interference lines gra.setcolor (Color.Black); Random ran = new random (); for (int i = 0; i < 70;i++) {int x = Ran.nextint (255); int y = ran.nextint (255); int x1 = Ran.nextint (255); int y1 = Ran.nex TInt (255); Gra.drawline (x, y,x+x1, y+y1);//Draw line}//generate verification code stringbuffer SB = new StringBuffer (); int r = 0,g = 0,b = 0; for (int i = 0; i < Codecount; i++) {String Strrand = string.valueof (Codesequence[ran.nextint (codesequence.length)]);// A different color is generated for each verification code. Add recognition system Difficulty r = Ran.nextint (255), G = ran.nextint (255), B = ran.nextint (255); Gra.setcolor (new Color (R, G, b)); Gra.drawstring ( Strrand, (i+1) *13, height-4); Sb.append (Strrand);} try {imageio.write (buffered, "JPEG", Output),} catch (Exception e) {throw new RuntimeException (e);} return sb.tostring ();}}
2:servlet using a verification code
protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {//Disable image caching C5/>resp.setheader ("Pragma", "No-cache"); Resp.setheader ("Cache-control", "No-cache"); Resp.setdateheader ("Expires", 0); Resp.setcontenttype ("Image/jpeg"); Generate the captcha image string veriycode = Veriycodeutils.newveriycode (Resp.getoutputstream ());//Save the Verification code to the session HttpSession Session = Req.getsession (); Session.setattribute ("Validatecode", Veriycode);}
3:jsp page Use verification code
3: Check the verification codeprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException { HttpSession session = Req.getsession (); Object Validatecode = Session.getattribute ("Validatecode"); System.out.println (Validatecode); String codevify = Req.getparameter ("codevify"), if (Codevify==null | | codevify.equals ("")) {Req.setattribute ("code", " The verification certificate cannot be empty "); Req.getrequestdispatcher ("/index.jsp "). Forward (req, resp); return;} else if (!validatecode.tostring (). Equalsignorecase (codevify)) {Req.setattribute ("code", "certificate Error"); Req.getrequestdispatcher ("/index.jsp"). Forward (req, resp); return;} System.out.println ("Start doing other business operations .....");
the calibration diagram is as follows:
Jsp+servlet Verification Code Case