Java web verification code and java web Verification Code
Verification Code: When logging on to or registering a system, users are required to enter a verification code to distinguish user behavior from computer program behavior. The purpose is to prevent malicious registration and brute force password cracking.
How to Implement the verification code: Use the server to randomly generate numbers and letters to form an image. Use the jsp page to display the verification code and the user input the verification code. Then use the server class to obtain the image and user input data respectively, determine whether the two data items are consistent.
Code Implementation
1. Write numbers and server classes randomly generated in English. Source Code:
1 package com; 2 3 import java. awt. color; 4 import java. awt. font; 5 import java. awt. graphics; 6 import java. awt. image. bufferedImage; 7 import java. io. byteArrayOutputStream; 8 import java. io. IOException; 9 import java. io. printWriter; 10 11 import javax. imageio. imageIO; 12 import javax. servlet. servletException; 13 import javax. servlet. servletOutputStream; 14 import javax. servlet. http. httpServlet; 15 import javax. servlet. http. httpServletRequest; 16 import javax. servlet. http. httpServletResponse; 17 import javax. servlet. http. httpSession; 18 19 public class logcheck extends HttpServlet {20 21 public logcheck () {22 super (); 23} 24 25 26 public void destroy () {27 super. destroy (); 28} 29 30 public void doGet (HttpServletRequest request, HttpServletResponse response) 31 throws ServletException, IOException {32 33 doPost (request, response ); 34} 35 36 37/* Core Code implemented */38 public void doPost (HttpServletRequest request, HttpServletResponse response) 39 throws ServletException, IOException {40 41 response. setContentType ("image/jpeg"); 42 HttpSession session = request. getSession (); 43 int width = 60; 44 int height = 20; 45 46 // set the browser not to cache this image 47 response. setHeader ("Pragma", "No-cache"); 48 response. setHeader ("Cache-Control", "no-cache"); 49 response. setDateHeader ("Expires", 0); 50 51 // create a memory image and obtain the image context 52 BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); 53 Graphics g = image. getGraphics (); 54 55/* 56 * generate random verification code 57 * sequence table 58 */59 String chars = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ "; 60 char [] rands = new char [4]; 61 for (int I = 0; I <4; I ++) {62 int rand = (int) (Math. random () * 36); 63 rands [I] = chars. charAt (rand); 64} 65 66/* 67 * generated image 68 * painted background 69 */70g. setColor (new Color (0 xDCDCDC); 71g. fillRect (0, 0, width, height); 72 73/* 74 * random generation of 120 interference points 75 */76 77 for (int I = 0; I <120; I ++) {78 int x = (int) (Math. random () * width); 79 int y = (int) (Math. random () * height); 80 int red = (int) (Math. random () * 255); 81 int green = (int) (Math. random () * 255); 82 int blue = (int) (Math. random () * 255); 83g. setColor (new Color (red, green, blue); 84g. drawOval (x, y, 1, 0); 85} 86g. setColor (Color. BLACK); 87g. setFont (new Font (null, Font. ITALIC | Font. BOLD, 18); 88 89 // different characters of the Verification Code at different heights 90 GB. drawString ("" + rands [0], 1, 17); 91g. drawString ("" + rands [1], 16, 15); 92g. drawString ("" + rands [2], 31, 18); 93g. drawString ("" + rands [3], 46, 16); 94g. dispose (); 95 96 // upload the image to the client 97 ServletOutputStream sos = response. getOutputStream (); 98 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 99 ImageIO. write (image, "JPEG", baos); 100 byte [] buffer = baos. toByteArray (); 101 response. setContentLength (buffer. length); 102 sos. write (buffer); 103 baos. close (); 104 sos. close (); 105 106 session. setAttribute ("checkcode", new String (rands); 107} 108 109 110 public void init () throws ServletException {111 // Put your code here112} 113 114}
2. The page for displaying the verification code:
1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8
3. Check whether the entered verification code is correct:
1 package com; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 6 import javax. jms. session; 7 import javax. servlet. servletException; 8 import javax. servlet. http. httpServlet; 9 import javax. servlet. http. httpServletRequest; 10 import javax. servlet. http. httpServletResponse; 11 import javax. servlet. http. httpSession; 12 13 public class yanzheng extends HttpServlet {14 15 public yanzheng () {16 super (); 17} 18 19 public void destroy () {20 super. destroy (); 21} 22 23 public void doGet (HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException {25 26 doPost (request, response ); 27} 28/* Core code */29 public void doPost (HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException {31 32 String info = null; 33/* obtain the input value */34 String value1 = request. getParameter ("name"); 35 36/* Get the image value */37 HttpSession session = request. getSession (); 38 String value2 = (String) session. getAttribute ("checkcode"); 39 40/* compare two values (uppercase and lowercase letters) */41 if (value2.20.signorecase (value1) {42 info = "the verification code is entered correctly "; 43} else {44 info = "Incorrect verification code"; 45} 46 System. out. println (info); 47 request. setAttribute ("info", info); 48 request. getRequestDispatcher ("/login. jsp "). forward (request, response); 49} 50 51 52 public void init () throws ServletException {53 // Put your code here54} 55 56}
4. The input structure page is displayed (whether the verification code is correct ):
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8
5. Project Structure and effect: