Forms
<form action="loginServlet" method="post"> 请输入验证码:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button> </form>
When the page is loaded, it will automatically request getCodeServlet, get the picture (verification code).
getCodeServlet, generating codes
. 1 @WebServlet ( "/ getCodeServlet" ) 2 public class GetCodeServlet the extends the HttpServlet { . 3 // codes width and height . 4 Private static int WIDTH = 80 ; . 5 Private static int HEIGHT = 25 ; . 6 . 7 // draw the background . 8 Private void drawBg (Graphics G) { . 9 // RGB 10 g.setColor ( new new Color (128, 128, 128 )); . 11 // draw a rectangle. X, Y, wigth, height 12 is g.fillRect (0,0 , WIDTH, HEIGHT); 13 is // random interference map 100 points 14 the Random Random = new new the Random (); 15 for ( int I = 0; I <100; I ++ ) { 16 // generated the decimal (0,1), * WIDTH | HEIGHT , then rounding will do . 17 int X = random.nextInt (WIDTH); 18 is int Y = random.nextInt (HEIGHT); . 19 g.drawOval (X, Y, 1,1 ); 20 is 21 is // color interference point may be randomly, randomly generated red, green, blue can 22 is // g.setColor (new new color (red, green, blue)); 23 is } 24 } 25 26 is 27 // Draw codes 28 Private void drawCode (G Graphics, char [] code) { 29 g.setColor (as Color.BLACK.); 30 // font, style (when a plurality of vertical partition), size 31 g.setFont ( new new the Font ( "serif", Font.ITALIC | Font.BOLD, 18 is )); 32 // draw character codes, parameters at different locations: to draw a String, horizontal and vertical coordinates. + "" To turn char String. 33 is g.drawString (code [0] + "", 1,17 ); 34 is g.drawString (code [. 1] + "", 16,15 ); 35 g.drawString (code [2] + "", 31 is , 18 ); 36 g.drawString (code [. 3] + "", 46,16 ); 37 [ } 38 is 39 // randomly generates four codes 40 Private char [] the getCode () { 41 is String chars = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm" ; 42 is char [] = code new new char [. 4 ]; 43 is the Random Random = new new the Random (); 44 is for ( int I = 0; I <. 4; I ++ ) { 45 // [0,62) 46 is int index = random.nextInt (62 is ) ; 47 code[i]=chars.charAt(index); 48 } 49 return code; 50 } 51 52 53 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { 54 HttpSession session = request.getSession(); 55 ServletOutputStream sos = response.getOutputStream(); 56 response.setContentType("image/jpeg"); 57 58 //设置浏览器不缓存此图片 59 response.setHeader("Pragma","No-cache"); 60 response.setHeader ( "the Cache-Control", "Cache-NO" ); 61 is . Response.setDateHeader ( "the Expires", 0 ); 62 is 63 is // Create image memory 64 the BufferedImage BufferedImage = new new the BufferedImage (WIDTH, HEIGHT, TYPE_INT_RGB that) ; 65 Graphics G = bufferedImage.getGraphics (); 66 char [] code = the getCode (); 67 // the session codes in a domain. To obtain a session object prior to submission response 68 session.setAttribute ( "code", new new String (code)); 69 drawBg (G); 70 drawCode (G, code); 71 g.dispose(); 72 73 //将图片输出到浏览器 74 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 75 ImageIO.write(bufferedImage,"JPEG",baos); 76 baos.writeTo(sos); 77 baos.close(); 78 sos.close(); 79 } 80 81 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 82 doPost(request,response); 83 } 84 }
loginServlet, form processing
1 @WebServlet("/loginServlet") 2 public class LoginServlet extends HttpServlet { 3 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 4 response.setContentType("text/html;charset=utf-8"); 5 HttpSession session = request.getSession(); 6 String trueCode= (String) session.getAttribute("code"); 7 String code=request.getParameter("code"); 8 9 if (code.equals(trueCode)){ 10 response.getWriter().write("验证码正确"); 11 } 12 else { 13 response.getWriter().write("验证码错误"); 14 } 15 } 16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 doPost(request,response); 19 } 20 }
The above approach is case sensitive codes.
not case sensitive:
// first converted to all uppercase | lowercase, and then determine trueCode = trueCode.toLowerCase (); code = code.toLowerCase (); // trueCode = trueCode.toUpperCase (); // code = trueCode.toUpperCase ();