Specific writing of entity classes
Package Com.han.model;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse; public class Validatecodeservlet extends HttpServlet {/** * */private static final long Serialversionuid = 23769
92603034716655L; Private final Font mfont = new Font ("Arial black", Font.plain, 15); Set font private final int linewidth = 2; Length of the jamming line =1.414*linewidth private final int width = 88; Define graphic size private final int height = 25;
Define graphic size private final int count = 200; /** * Description: * * @param FC * Description: * @param BC * Description: * * @return Description: * * Private Colo
R Getrandcolor (int FC, int BC) {//Get a given range of random colors final Random Random = new Random (); if (FC > 255) {fc = 255;
} if (BC > 255) {BC = 255;
Final int r = FC + Random.nextint (BC-FC);
Final int g = FC + Random.nextint (BC-FC);
Final int b = FC + Random.nextint (BC-FC);
return new Color (R, G, b); //Process post @Override public void DoPost (final httpservletrequest request, Final httpservletresponse response) thro
WS Servletexception, IOException {doget (request, response); /** * Description: * * @param request * Description: * @param response * Description: * * @throws Servlet Exception * Description: * @throws IOException * Description: * * @Override public void doget (final httpser
Vletrequest request, final HttpServletResponse response) throws Servletexception, IOException {response.reset ();
Set page does not cache Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);
Response.setcontenttype ("Image/gif");
Create an image in memory Final BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Get graphics Context final graphics2d g = (graphics2d) image.getgraphics ();
Generate Random class final Random Random = new Random (); Set Background color G.setcolor (Getrandcolor (200, 250));
---1 g.fillrect (0, 0, width, height);
Set Font G.setfont (Mfont); Draw a Border G.setcolor (Getrandcolor (0, 20));
---2//Distance g.drawrect (0, 0, width-1, height-1); Randomly generated interference line, so that the authentication code in the image is not easily detected by other programs for (int i = 0; i < count; i++) {G.setcolor (Getrandcolor));//---3 f inal int x = Random.nextint (width-linewidth-1) + 1;
Guaranteed to draw within the frame final int y = random.nextint (height-linewidth-1) + 1;
Final int xl = Random.nextint (linewidth);
Final int yl = Random.nextint (linewidth);
G.drawline (x, y, X + xl, y + yl);
//Take the randomly generated authentication code (4 digits) String SRand = "";
for (int i = 0; i < 4; i++) {final String rand = string.valueof (Random.nextint (10));
SRand + rand; The authentication CodeShown to the image, the calling function comes out with the same color, possibly because the seed is too close, so you can only generate G.setcolor directly (new Color (130), Random.nextint + random. Nextint (130), 2 0 + random.nextint (130)));
--4--50-100//The first parameter is to be drawn to the string after two parameters are directed to (0,0) x axis and Y axis g.drawstring (rand, (i) + 10, 20);
//The authentication code is deposited in session request.getsession () setattribute ("Validatecode", SRand);
Image effective//its role is to destroy the program specified in the graphical interface resources, if the use of graphics to obtain some of the graphics resources of Windows, without the shutdown, due to the later use of many people will cause memory overflow situation, resulting in program card death.
G.dispose ();
Final Java.io.OutputStream OS = Response.getoutputstream ();
Output image to page imageio.write (image, "PNG", OS);
Os.flush ();
Os.close ();
}
}
Configuration of Web.xml files
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <welcome-file-list> <welcome-file>index.jsp</ welcome-file> </welcome-file-list> <servlet> <servlet-name>validatecodeservlet</servlet-n ame> <servlet-class>com.han.model.ValidateCodeServlet</servlet-class> <init-param> <par am-name>width</param-name> <param-value>200</param-value> </init-param> <init-param > <param-name>height</param-name> <param-value>80</param-value> </init-param> ;init-param> <param-name>codeCount</param-name> <param-value>5</param-value> </ Init-param> </servlet> <servlet-maPping> <servlet-name>ValidateCodeServlet</servlet-name> <url-pattern>/validatecodeservlet&
Lt;/url-pattern> </servlet-mapping> </web-app>
JSP page
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >