The code looks like this:
Copy Code code as follows:
Package com.hoo.util;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* <b>function:</b> Verification Code generation tool class
* @project NetworkService
* @package Com.hoo.util
* @fileName Validcodeutils.java
* @createDate 2010-8-3 03:05:50
* @author Hoojo
*/
@SuppressWarnings ("unused")
public class Validcodeutils {
/*********************************************************************
* Verification Code width
*/
public static int WIDTH = 60;
/***
* Verification Code Height
*/
public static int HEIGHT = 20;
/**********************************************************************
* Verify code background color COLOR_FC_BG should be less than COLOR_BC_BG
*/
public static int COLOR_FC_BG = 200;
/***
* Verify code background color COLOR_FC_BG should be less than COLOR_BC_BG
*/
public static int color_bc_bg = 250;
/**********************************************************************
* Verify code background interference Line Color color_fc_line should be less than color_bc_line
*/
public static int color_fc_line = 160;
/***
* Verify code background interference Line Color color_fc_line should be less than color_bc_line
*/
public static int color_bc_line = 200;
/***************************************************************************
* Verify code color Color_fc_code should be less than color_bc_code
*/
public static int color_fc_code = 20;
/***
* Verify code color Color_fc_code should be less than color_bc_code
*/
public static int color_bc_code = 170;
/***************************************************************************
* Generate a color within a specified range
* @param FC Range FC color value is less than 255
* @param BC range BC color value less than 255
* @return Color
*/
private static Color getrandcolor (int fc, int BC) {
Random Random = new Random ();
if (FC < 0)
FC = 0;
if (BC < 0)
BC = 1;
if (FC > 255)
FC = 255;
if (BC > 255)
BC = 255;
if (bc = = FC)
BC + 10;
int temp = 0;
If (BC < FC) {
temp = BC;
BC = FC;
FC = temp;
}
int r = FC + Random.nextint (BC-FC);
int g = FC + Random.nextint (BC-FC);
int B = FC + Random.nextint (BC-FC);
return new Color (R, G, b);
}
/**
* <b>function:</b> Generate Picture method
* @createDate 2010-8-3 03:06:22
* @author Hoojo
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Boolean
* @throws Exception
*/
public static Boolean getImage (HttpServletRequest request, httpservletresponse response) throws exception{
Response.reset ();
Response.setcontenttype ("Image/jpeg");
Set Page Not Cached
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Create an image in memory
BufferedImage image = New BufferedImage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb);
Get Graphics context
Graphics img = image.getgraphics ();
Generate Random Class
Random Random = new Random ();
Set Background color
Img.setcolor (Getrandcolor (COLOR_FC_BG, COLOR_BC_BG));
Img.fillrect (0, 0, WIDTH, HEIGHT);
Set font
Img.setfont (New Font ("Times New Roman", Font.plain, 18));
Draw a border
G.setcolor (New Color ());
G.drawrect (0,0,width-1,height-1);
Randomly generated 155 lines of interference, so that the image of the authentication code is not easily detected by other programs
Img.setcolor (Getrandcolor (Color_fc_line, color_bc_line));
for (int i = 0; i < i++) {
int x = Random.nextint (WIDTH);
int y = random.nextint (HEIGHT);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
Img.drawline (x, y, X + xl, y + yl);
}
Take a randomly generated authentication code (4 digits)
String codevalue = "";
for (int i = 0; i < 4; i++) {
String rand = string.valueof (Random.nextint (10));
String rand = Getrandomchar ();
Codevalue = Codevalue.concat (rand);
Img.setfont (Getrandomfont ());//Random font
Display the authentication code in the image
Img.setcolor (Getrandcolor (Color_fc_code, Color_bc_code));
Img.drawstring (Rand, * i + 6, 16);
}
Request.getsession (). setattribute ("Codevalue", Codevalue);
Image entry into force
Img.dispose ();
Output image to Page
return Imageio.write (Image, "JPEG", Response.getoutputstream ());
}
/**
* Randomly generated characters, including uppercase, lowercase, digital
* <b>function:</b> function
* @createDate 2010-8-23 10:33:55
* @author Hoojo
* @return
*/
public static String Getrandomchar () {
int index = (int) math.round (Math.random () * 2);
String Randchar = "";
Switch (index) {
Case 0://Uppercase characters
Randchar = string.valueof ((char) Math.Round (Math.random () * 25 + 65));
Break
Case 1://Lowercase characters
Randchar = string.valueof ((char) Math.Round (Math.random () * 25 + 97));
Break
default://Digital
Randchar = string.valueof (Math.Round (Math.random () * 9));
Break
}
return Randchar;
}
/**
* <b>function:</b> randomly generated font, text size
* @createDate 2010-8-23 10:44:22
* @author Hoojo
* @return
*/
public static Font Getrandomfont () {
string[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"};
int fontindex = (int) math.round (Math.random () * (fonts.length-1));
int fontsize = (int) math.round (Math.random () * 4 + 16);
return new Font (Fonts[fontindex], Font.plain, fontsize);
}
}
Where the value of the CAPTCHA is saved in session: Request.getsession (). setattribute ("Codevalue", Codevalue);
Compares the values entered by the user and whether the Codevalue in session is equal;
Here is the JSP page call Servlet:ValidCodeServlet.java
The above Validcodeutils Authentication Code generation tool class is called in Validcodeservlet
Package com.hoo.servlet;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Com.hoo.util.ValidCodeUtils;
@SuppressWarnings ("Serial")
public class Validcodeservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
try {
Validcodeutils.getimage (request, response);
catch (Exception e) {
E.printstacktrace ();
}
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
JSP page calls the Servlet method can
Js:reloadvalidcode method
function Reloadvalidcode (o) {
O.SRC = "${pagecontext.request.contextpath}/validcodeservlet?timed=" + New Date (). getmilliseconds ();
}
Here the "timed=" + New Date (). Getmilliseconds () is required to prevent IE caching
HTML Tags:
The URL that is configured directly with the servlet name corresponds to the Web.xml configuration. The main call path ${pagecontext.request.contextpath}/validcodeservlet This will take the root directory, more insurance.
Validcodeservlet Configuration in Web.xml
<servlet>
<servlet-name>validCodeServlet</servlet-name>
<servlet-class>com.hoo.servlet.ValidCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validCodeServlet</servlet-name>
<url-pattern>/validCodeServlet</url-pattern>
</servlet-mapping>