Java Random verification?
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;
// Before servlet3.0, it must be configured in web. xml.
validateColorServlet
com.atguigu.javaweb.ValidateColorServlet
validateColorServlet
/validateColorServlet
// After servlet3.0, you can directly use @ WebServlet,
@ WebServlet (/validateColorServlet) public class ValidateColorServlet extends HttpServlet {public static final String CHECK_CODE_KEY = CHECK_CODE_KEY; private static final long serialVersionUID = 1L; // you can specify the width, height, and, number of verification codes private int width = 152; private int height = 40; private int codeCount = 6; // height of the Verification Code font private int fontHeight = 4; // a single character baseline in the verification code. that is, a single character in the verification code is located in the (codeX, codeY) position in the upper-left corner of the Verification Code image. private int codeX = 0; private int codeY = 0; // which characters constitute the verification code char [] codeSequence = require (); // initialize the verification code graphic attribute public void init () {fontHeight = height-2; codeX = width/(codeCount + 2); codeY = height-4;} public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// define a type of BufferedImage. bufferedImage buffImg = null; buffImg = new BufferedImage (width, height, BufferedImage. TYPE_3BYTE_BGR); // create a Graphics2D image Graphics2D graphics = null in buffImg; graphics = buffImg. createGraphics (); // sets a color so that subsequent graphics Of The Graphics2D object use this color graphics. setColor (Color. WHITE); // fill in a specified rectangle: x-x coordinate of the rectangle to be filled; y-y coordinate of the rectangle to be filled; width-width of the rectangle to be filled; height-the height graphics of the rectangle to be filled. fillRect (0, 0, width, height); // create a Font object: name-Font name; style-Font style Constant; size-Font point size Font font = null; font = new Font (, Font. BOLD, fontHeight); // use this font for subsequent graphics of Graphics2D objects. setFont (font); graphics. setColor (Color. BLACK); // draw the border of the specified rectangle. The drawn rectangle is a pixel higher than the width of the component. drawRect (0, 0, width-1, height-1); // generates 15 Random interference lines, making it difficult for other programs to detect 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 (20); int y1 = random. nextInt (20); graphics. drawLine (x, y, x + x1, y + y1);} // creates a randomCode object to save the randomly generated Verification Code, so that the user can verify StringBuffer randomCode after logging on; randomCode = new StringBuffer (); for (int I = 0; I <codeCount; I ++) {// obtain the randomly generated verification code number String strRand = null; strRand = String. valueOf (codeSequence [random. nextInt (36)]); // puts the generated random characters into the StringBuffer randomCode. append (strRand); // use a random color to draw the verification code into the image graphics. setColor (Color. BLUE); graphics. drawString (strRand, (I + 1) * codeX, codeY);} // put the string corresponding to the StringBuffer with all the random characters into the HttpSession request. getSession (). setAttribute (CHECK_CODE_KEY, randomCode. toString (); // disable image cache 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 ();}}