Web-based online bookstore system Development-----Login Registration Extension-------CAPTCHA function

Source: Internet
Author: User
Tags browser cache

public class Checkcode extends HttpServlet {private static final long serialversionuid = 1l;private int width = 80;//Verification Code The width private int height = 20 of the picture; The high private int codecount = 4 of the captcha picture; Number of characters in the captcha picture private int x = 16;private int fontheight = 16;private int codey = 18;private final char[] 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 '};p rotected void doget (HttpServletRequest request, Httpservletrespon SE response) throws Servletexception, java.io.IOException {//Constructs a type of bufferedimage of one of the predefined image types, sets the width, height, and type of the image (Type_int_ RGB) BufferedImage Img = new BufferedImage (width, height, bufferedimage.type_int_rgb);//returns GRAPHICS2D, The Graphics2D class extends from the graphics class to provide control of geometric shapes, coordinate transformations, color management, and text layout more complex graphics g = img.getgraphics (); Random random = new random ()//fills the image as a white g.setcolor (color.white);//fills the specified rectangle. x, y coordinates are 0, width is width, height is heightg.fillrect (0, 0, width, height);//create font, font sizeShould depend on the height of the picture. Font font = new Font ("Times New Roman", Font.plain, Fontheight); G.setcolor (Color.Black); G.setfont (font); Color Junefont = new Color (153, 204, 102);//randomly generates 130 lines of interference that are not easily detectable by other programs G.setcolor (Junefont); for (int i = 0; i <; i++) {/ /return pseudo-random number int x = random.nextint (width); int y = random.nextint (height); int xl = Random.nextint (16); 80/5=16int yl = Random.nextint (16);//In the coordinate system of the graphics context, use the current color to draw a line between the points (x1, y1) and (x2, y2) g.drawline (x, y, X + xl, y + yl); }//Randomcode is used to save randomly generated verification codes so that the user is authenticated after login, thread-safe variable character sequences stringbuffer Randomcode = new StringBuffer ();// Random generation of Codecount numbers for the verification code for (int i = 0; i < Codecount; i++) {//returns the string representation of the char parameter string strrand = String.valueof (Codesequen Ce[random.nextint (36)]);//Use a randomly generated color to draw the verification code into the image//create an opaque SRGB color with the specified red, green, and blue values, all within the range of (0-255) G.setcolor (The new color ( + random.nextint (+ +), + random.nextint (+), + random.nextint (110));//The text given by the specified string is drawn using the current font and color of this graphics context. The baseline of the leftmost character is located at the (x, y)//position of this graphics context coordinate system. g.DrawString (Strrand, (i + 1) * x-4, Codey); RANDOMCOde.append (Strrand);} HttpSession session = Request.getsession (); Save the four-digit verification code to the session session.setattribute ("Realcode", randomcode.tostring ());//Disable browser cache Response.setheader (" Pragma "," No-cache "); HTTP 1.0response.setheader ("Cache-control", "No-cache");//HTTP 1.1response.setdateheader ("Expires", 0); Prevent buffering Response.setcontenttype ("Image/gif") on the proxy server side; Setting the content type of the response being sent to the client//outputting the image to the servlet output stream, Servletoutputstream provides an output stream to send binary data to the client Servletoutputstream SOS = Response.getoutputstream (); Imageio.write (Img, "GIF", SOS); Write an image to//Outputstreamsos.flush () using any imagewriter that supports a fixed type; Refreshes this output stream and forces all buffered output bytes to be written out sos.close ();}

  

Web-based online bookstore system Development-----Login Registration Extension-------CAPTCHA function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.