Java basics-> Use of verification codes, java basics-verification Codes
A verification code is a public, fully automated program that distinguishes users from computers and people. It can prevent malicious password cracking, ticket flushing, and Forum bumping, effectively preventing a hacker from continuously logging on to a specific registered user using brute force cracking methods of specific programs, in fact, the verification code is now used by many websites (such as China Merchants Bank's personal online bank and Baidu community). We have implemented this function in a relatively simple way. Today, we use Java to implement a simple verification code program.
Verification Code Program implemented in Java
To generate a verification code:
Verification code verification steps:
We create a Java Web project with the following structure:
1. Define an index. jsp to refresh the verification code, enter the verification code, and submit the verification code:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
2. ImageServlet. java is used to generate a verification code image and save the verification code to the session:
package com.huhx.servlet;import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author huhx */@WebServlet("/servlet/ImageServlet")public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage bufferedImage = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.getGraphics(); Color color = new Color(225, 230, 246); graphics.setColor(color); graphics.fillRect(0, 0, 68, 22); char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); Random random = new Random(); int length = chars.length; StringBuffer buffer = new StringBuffer(); int index; for (int i = 0; i < 4; i++) { index = random.nextInt(length); graphics.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255))); graphics.drawString(chars[index] + "", (i * 15) + 3, 18); buffer.append(chars[index]); } request.getSession().setAttribute("code", buffer.toString()); ImageIO.write(bufferedImage, "JPG", response.getOutputStream()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
3. LoginServlet. java, used to receive the user's verification code for submission and verification:
Package com. huhx. servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** @ author huhx */@ WebServlet ("/servlet/LoginServlet") public class LoginServlet extends HttpServlet {private Static final long serialVersionUID = 1L; protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String codeCheck = request. getParameter ("codeCheck"); String raelCode = (String) request. getSession (). getAttribute ("code"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter out = response. getWriter (); if (codeCheck. toUpperCas E (). equals (raelCode. toUpperCase () {out. println ("Verification Code successful! ");} Else {out. println (" Verification Code failed! ");} Out. flush (); out. close ();} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Note: eclipse uses the @ WebServlet annotation method. If it is another method, the servlet needs to define the ing relationship in web. xml.
4. The running result is as follows:
Other verification code frameworks: kaptcha and jcaptcha. If you are interested, take a look.