About JSP verification code generation, display, validation and refresh: 1: Four-bit data validation picture generation, implemented in servlet
2: Configuring Servlet in Web.xmlli
3: Display a servlet-generated picture on the page
4: Verification of verification code input data
5: The verification code of the refresh
——————————————————————–
1: Four-bit data validation image generation, implemented in servlet, servlet code as follows:
Import java.io.*;
Import javax.servlet.http.*;
Import javax.servlet.*;
Import java.util.*;
Import java.awt.*;
Import java.awt.image.*;
Import javax.imageio.*;
public class Vimage extends HttpServlet {
public void init (ServletConfig conf) throws Servletexception {
Super.init (conf);
}
public void Doget (HttpServletRequest req, httpservletresponse Res) throws Servletexception, IOException {
Res.setcontenttype ("Image/jpeg");
Res.setheader ("Pragma", "No-cache");
Res.setheader ("Cache-control", "No-cache");
Res.setdateheader ("Expires", 0);
HttpSession session = Req.getsession ();
Create an image in memory
int width =, height = 20;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Get Graphics context
Graphics g = image.getgraphics ();
Generate Random Class
Random Random = new Random ();
Set Background color
G.setcolor (Getrandcolor (200, 250));
G.fillrect (0, 0, width, height);
Set font
G.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
G.setcolor (Getrandcolor (160, 200));
for (int i = 0; I 255) FC = 255;
if (BC > 255) BC = 255;
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);
}
}
2: Configure servlet in Web.xmlli:
Vimage
/vimage
Note: Note the position of the element in the Web.xml file
3: Display a servlet-generated picture on the page:
Note: Add this to the place where you need to display the verification code, and name will be used when the verification code is refreshed.
4: Verification code input data validation:
Verification code generated in the session, is the servlet in this sentence Session.setattribute ("Post_validate_code", SRand), the specific verification is not much to say.
5: The verification code of the refresh
Refresh Verification Code
function reloadimage (URL)
{
Document.form name. pic.src = URL;
}