Login (with verification code image) small case, verification code case
1 <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%> 2
1 @ WebServlet (name = "VerifyCodeServlet", expiration = "/VerifyCodeServlet") 2 public class VerifyCodeServlet extends HttpServlet {3 protected void doGet (HttpServletRequest request, response) 4 throws ServletException, IOException {5/* 6*1. Save the image 7*2. Save the text on the image to the session domain 8*3. Send the image to the client 9 */10 VerifyCode vc = new VerifyCode (); 11 BufferedImage image = vc. getImage (); 12 request. getSession (). setAttribute ("session_vcode", vc. getText (); // Save the text on the image to the session domain 13 14 try {15 VerifyCode. output (image, response. getOutputStream (); 16} catch (Exception e) {17 e. printStackTrace (); 18} 19} 20}
1/* 2 * Verification Code Image Generation Method 3 **/4 public class VerifyCode {5 private int w = 70; 6 private int h = 35; 7 private Random r = new Random (); 8 // optional font 9 private String [] fontNames = {"", "文 "," ", "", "_ GB2312"}; 10 // optional characters 11 private String codes = "23456789 bytes"; 12 private Color bgColor = new Color (255,255,255 ); 13 private String text; 14 15 // generate random Color 16 private Color randomColor () {17 int red = r. nextInt (150); 18 int green = r. nextInt (150); 19 int blue = r. nextInt (150); 20 return new Color (red, green, blue); 21} 22 23 // generate random Font 24 private Font randomFont () {25 int index = r. nextInt (fontNames. length); 26 String fontName = fontNames [index]; // generate a random font name 27 int style = r. nextInt (4); // generate a random style, 0 (no style), 1 (BOLD), 2 (italic), 3 (bold and italic) 28 int size = r. nextInt (5) + 25; // generate a random Font size of 24-2829 return new Font (fontName, style, size ); 30} 31 32 // draw interference Line 33 private void drawLine (BufferedImage image) {34 int num = r. nextInt (6); 35 Graphics2D g2 = (Graphics2D) image. getGraphics (); 36 for (int I = 0; I <num; I ++) {// generates coordinates of two points, that is, four values: 37 int x1 = r. nextInt (w); 38 int y1 = r. nextInt (h); 39 int x2 = r. nextInt (w); 40 int y2 = r. nextInt (h); 41 g2.setStroke (new BasicStroke (1.5F); 42 g2.setColor (Color. BLUE); // The interference line is BLUE 43 g2.drawLine (x1, y1, x2, y2 ); // draw line 44} 45} 46 47 // generate a random string of 48 private char randomChar () {49 int index = r. nextInt (codes. length (); 50 return codes. charAt (index); 51} 52 53 // create a BufferedImage54 private BufferedImage creatImage () {55 BufferedImage image = new BufferedImage (w, h, BufferedImage. TYPE_INT_RGB); 56 Graphics2D g2 = (Graphics2D) image. getGraphics (); 57 g2.setColor (this. bgColor); 58 g2.fillRect (0, 0, w, h); 59 return image; 60} 61 62 // call this method to obtain the verification code 63 public BufferedImage getImage () {64 BufferedImage image = creatImage (); // create an image buffer 65 Graphics2D g2 = (Graphics2D) image. getGraphics (); // obtain the drawing environment 66 StringBuilder sb = new StringBuilder (); // used to load the generated Verification Code text 67 for (int I = 0; I <4; I ++) {// cyclically four times, each time a random String of 68 s = randomChar () + "; // a random String of 69 sb is generated. append (s); // Add the letter to sb 70 float x = I * 1.0F * w/4; // set the X axis coordinate of the current character 71 g2.setFont (randomFont (); // set the random font 72 g2.setColor (randomColor (); // set the random color 73 g2.drawString (s, x, h-5); // drawing 74} 75 this. text = sb. toString (); // assign the generated string to this. text76 drawLine (image); // Add the interference line 77 return image; 78} 79 // return the text 80 public String getText () {81 return text on the Verification Code image; 82} 83 // Save the image to the specified output stream 84 public static void output (BufferedImage image, OutputStream out) 85 throws Exception {86 ImageIO. write (image, "JPEG", out); 87} 88}
1 <% @ page contentType = "text/html; charset = UTF-8 "language =" java "%> 2