Java Background Code (CHECKCODESERVLET.JAVA)
Packageweb;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJava.io.PrintWriter;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classCheckcodeservletextendsHttpServlet {/** Return a live picture **/ Public voidService (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/jpeg"); //Get byte streamOutputStream os=Response.getoutputstream (); //image Map Area, understand the board on the line, draw verification code//bufferedimage image=new bufferedimage (width,height,imagetype);BufferedImage image=NewBufferedImage (80,40, Bufferedimage.type_int_rgb); //BrushesGraphics g=Image.getgraphics (); //Add background color (random)Random r=NewRandom (); G.setcolor (NewColor (R.nextint (R.nextint), R.nextint (256));//color (red,green,blue); RGB format Colors SettingsG.fillrect (0, 0, 80, 40);//draw a rectangular area//Draw a StringG.setcolor (Color.Black);//set the color of the verification codeG.setfont (NewFont (NULL, font.bold,20));//Set the font fonts (font, style, size) of the captchaString Num=getnum (5);//Incoming Verification Codeg.DrawString (NUM, 5, 30);//(string content, x-coordinate, y-coordinate)//scribing, interfering with the robot identification verification Code for(inti=0;i<6;i++){ //Draw 6 LinesG.setcolor (NewColor (R.nextint (R.nextint), R.nextint (256));//set the color of a lineG.drawline (R.nextint), R.nextint, R.nextint (+), R.nextint (80));//G.drawline (start x coordinate, start y coordinate, end x coordinate, end point y coordinate); } //compressed transmission (the picture is larger, need to compress, draw a 24-bit graph)Imageio.write (image, "JPEG", OS); } //return the correct verification code//Length: Number of verification codes PublicString Getnum (intlength) {String str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";//constants, in order to pick out random verification codes based on lengthRandom r=NewRandom (); StringBuffer SF=NewStringBuffer (); for(inti=0;i<length;i++){ intIndex=r.nextint (Str.length ());//take the subscript of STR CharC=str.charat (index);//take the character according to the subscriptSf.append (c);//character into a string buffer } returnSf.tostring ();//Convert to String return }}
Web. XML configuration file
<servlet> <Servlet-name>Checkcodeservlet</Servlet-name> <Servlet-class>Web. Checkcodeservlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Checkcodeservlet</Servlet-name> <Url-pattern>/checkcode</Url-pattern> </servlet-mapping>
text.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Java Code Verification Code