Java implementation random authentication code function Instance code _java

Source: Internet
Author: User
Tags numeric value

Many systems now register, log in or publish information modules are added to the random code function, is to avoid automatic registration program or the use of automatic publishing programs.
The verification code is actually randomly chooses some characters to display in the picture the form on the page, if carries on the Tomato Garden XP system downloading submits the operation to need to simultaneously submit the picture the character, if submits the character and the server session saves differently, then considers submits the information to be invalid. In order to avoid automated program analysis and resolution of pictures, usually randomly generated in the picture some interference lines or distorted characters to increase the difficulty of automatic recognition.

Copy 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 picture.
private int width=60;
The height of the verification code picture.
private int height=20;
Number of verification code characters
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 validation picture properties
*/
public void Init () throws Servletexception
{
Get the initial information from the Web.xml
Width
String strwidth=this.getinitparameter ("width");
Height
String strheight=this.getinitparameter ("height");
Number of characters
String strcodecount=this.getinitparameter ("Codecount");
Converts the configured information to a numeric value
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 ();
To fill an image with white
G.setcolor (Color.White);
G.fillrect (0, 0, width, height);
To create a font, the size of the font should be based on the height of the picture.
Font font = new Font ("Fixedsys", Font.plain, Fontheight);
Sets the font.
G.setfont (font);
Draw a border.
G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);
Randomly generated 160 lines of interference, so that the image of the authentication code is not easily detected by other programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.