<span style= "FONT-SIZE:18PX;" >package com.java.process.jsp;import java.awt.color;import Java.awt.font;import java.awt.Graphics2D;import Java.awt.image.bufferedimage;import Java.io.ioexception;import Java.util.random;import Javax.imageio.ImageIO; Import Javax.servlet.servlet;import Javax.servlet.servletexception;import Javax.servlet.ServletOutputStream; Import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;</span>
<span style= "FONT-SIZE:18PX;" ></span>
<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "Java" >
<span style= "FONT-SIZE:18PX;" >//SERVLET3.0 must be configured in Web. XML </span>
<span style= "FONT-SIZE:18PX;" ></span><pre name= "code" class= "HTML" > <servlet> <servlet-name> Validatecolorservlet</servlet-name> <servlet-class>com.atguigu.javaweb.validatecolorservlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> Validatecolorservlet</servlet-name> <url-pattern>/validateColorServlet</url-pattern> </servlet-mapping>
<span style= "FONT-SIZE:18PX;" >//SERVLET3.0 can be used directly after the @webservlet,</span>
<span style= "FONT-SIZE:18PX;" > @WebServlet ("/validatecolorservlet") public class Validatecolorservlet extends HttpServlet {public static final String Check_code_key = "Check_code_key";p rivate static final Long Serialversionuid = 1l;//Set the width, height, and number of verification codes of the image private in T width = 152;private int height = 40;private int codecount = 6;//captcha font height private int fontheight = 4;//Verification Code single word's baseline line. That is: a single character in the Captcha is located at the upper-left corner of the captcha graphic (CodeX, Codey) at the location of private int codeX = 0;private int Codey = 0;//What characters the captcha consists of char [] codesequence = "A BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz23456789 ". ToCharArray ();//Initialize Authenticode graphic properties public void init () { Fontheight = Height-2;codex = width/(codecount + 2); Codey = height-4;} public void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {// Defines an image cache of type BUFFEREDIMAGE.TYPE_INT_BGR type BufferedImage buffimg = null;buffimg = new BufferedImage (width, height, BUFFEREDIMAGE.TYPE_3BYTE_BGR);//Create a graphics2d image graphics2d graphics in buffimg = Null;graphics = Buffimg.creategraphics ();//set a color to make subsequent graphics for GRAPHICS2D objects use this color Graphics.setcolor (color.white);// Fills a specified rectangle: X-The x-coordinate of the rectangle to fill; Y-The y-coordinate of the rectangle to fill; Width-the thickness of the rectangle to fill; Height-graphics.fillrect (0, 0, width, height) to fill the rectangle;//Create a Font object: name-the name of the font; Style-font-style constants; Size-font point size Font font = Null;font = new Font ("", Font.Bold, fontheight);//Make subsequent graphics of the Graphics2D object use this font Graphics.setfont (fo NT); Graphics.setcolor (color.black);//Draw the border of the specified rectangle, the rectangle drawn will be a pixel greater than the width of the component Graphics.drawrect (0, 0, width-1, height-1);// Randomly generates 15 interference lines, so that the authentication code in the image is not easy to be detected by other programs random random = Null;random = new Random (); Graphics.setcolor (Color.green); for (int i = 0; i < 55; i++) {int x = random.nextint (width), int y = random.nextint (height), int x1 = random.nextint (x); int y1 = Random.nextint (20); Graphics.drawline (x, y, x + x1, y + y1);} Create a Randomcode object that holds the randomly generated verification code so that the user logs on for verification stringbuffer Randomcode;randomcode = new StringBuffer (); for (int i = 0; i < Codecount; i++) {//Get a randomly generated captcha number string Strrand = Null;strrand = STring.valueof (Codesequence[random.nextint (36)]);//Put the random characters that are being generated into the StringBuffer randomcode.append (Strrand);// Draw the verification code into the image using the randomly generated color graphics.setcolor (color.blue); Graphics.DrawString (Strrand, (i + 1) * CodeX, Codey);} The string corresponding to the StringBuffer with all random characters is then placed into the HttpSession request.getsession (). SetAttribute (Check_code_key, Randomcode.tostring ());//Disable image Caching Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", " No-cache "); Response.setdateheader (" Expires ", 0);//Output the image to the output stream Servletoutputstream SOS = Null;sos = Response.getoutputstream (); Imageio.write (buffimg, "JPEG", SOS); Sos.close ();}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java random validation?