Nowadays, the registration, login or information publishing modules of many systems have added random code functions to avoid the use of automatic registration programs or automatic publishing programs.
The verification code actually randomly selects some characters and displays them on the page in the form of pictures. If you download and submit the Tomato Garden The submitted information is considered invalid. In order to prevent automatic programs from analyzing and parsing pictures, some interference lines are usually randomly generated on the picture or characters are distorted, making automatic recognition more difficult.
Copy the code code as follows:
package com.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Generate random verification code
* @author bitiliu
*
*/
public class ValidateCodeServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
//The width of the verification code image.
private int width=60;
//The height of the verification code image.
private int height=20;
//The number of characters in the verification code
private int codeCount=4;
private int x=0;
//font height
private int fontHeight;
private int codeY;
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 ' };
/**
* Initialize and verify image attributes
*/
public void init() throws ServletException
{
//Get initial information from web.xml
//width
String strWidth=this.getInitParameter("width");
//high
String strHeight=this.getInitParameter("height");
//Number of characters
String strCodeCount=this.getInitParameter("codeCount");
//Convert configured information into numerical values
try
{
if (strWidth!=null && strWidth.length()!=0)
{
width=Integer.parseInt(strWidth);
}
if (strHeight!=null && strHeight.length()!=0)
{
height=Integer.parseInt(strHeight);
}
if (strCodeCount!=null && strCodeCount.length()!=0)
{
codeCount=Integer.parseInt(strCodeCount);
}
}
catch(NumberFormatException e)
{}
x=width/(codeCount+1);
fontHeight=height-2;
codeY=height-4;
}
protected void service (HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
//Define image buffer
BufferedImage buffImg = new BufferedImage(
width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
//Create a random number generator class
Random random = new Random();
//Fill the image with white
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//Create a font. The size of the font should be determined according to the height of the image.
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
//Set font.
g.setFont(font);
//Draw a border.
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// Randomly generate 160 interference lines to make the authentication code in the image difficult to be detected by other programs.