This paper illustrates the generation and use of graphical verification code for Java Web development. Share to everyone for your reference. Specifically as follows:
The main purpose of the graphics verification code is to enhance the security, to increase the user to traverse all the possibilities to crack the password difficult.
The use of graphics verification Code includes the following 3 parts:
The generation of ① graphics verification Code;
The use of ② in the page;
③ verification;
1, the generation of graphics verification code
Assuming that the servlet generates a graphics CAPTCHA, the basic process generated in the JavaBean or JSP is the same. Design the following process:
① sets the document type of the response;
② generates random code;
③ to save the random code to the session;
④ generate pictures;
⑤ the random code to the memory picture;
⑥ to send the memory picture to the client;
1.1 Set the document type of the response
When you respond to a user, you need to set the document type, and to generate a picture document type can be set to: Image/gif.
The setting in the servlet is: Response.setcontenttype ("image/gif");
If you use it in a JSP page, you need to use: <%@ page contenttype= "Image/gif"%>
1.2 Generate random Code
Can be generated according to a variety of random number generation strategies, and you can set what the random code is composed of characters and the length of the random code.
The random code characters given in this paper are all letters and numbers. The random code generation strategy used is provided by the random object. The reference code is as follows:
Random code word characters:
public static final char[] Code = {' 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 ',
' 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 '
};
Code to generate random code:
StringBuffer checkcode=new StringBuffer ();
Once per loop, generate a for
(int i=0;i<code_length;i++)
{
int generated= (new Random ()). Nextint
Checkcode.append (code[generated]);
1.3 Save the random code to the session
In order to authenticate after the user submits the authentication code, the generated verification code needs to be saved in the session. In the servlet, you need to get the session object first and then use it.
Here is the reference code:
Saves the generated validation code to the session
HttpSession session=request.getsession (true);
Session.setattribute ("Checkcode", checkcode.tostring ());
1.4 Generating Pictures
Use the BufferedImage class to create the object, and then paint with the drawing object. Here is the reference code:
Create memory picture, parameter is picture size and type
bufferedimage image = new bufferedimage (49,14,BUFFEREDIMAGE.TYPE_INT_RGB);
Get Graphics handle
Graphics g = image.getgraphics ();
Sets the brush color
//G.setcolor (Color.yellow);
Draw Background
g.fillrect (0,1,49,12);
1.5 The random code display in the picture
refer to the following code:
//Set Font color
g.setcolor (color.black);
Draw Verification Code
g.drawstring (checkcode.tostring (), 4,11);
Image entry into force
g.dispose ();
1.6 Send the generated picture to the client
The reference code is as follows:
Copy Code code as follows:
Imageio.write (Image, "JPEG", Response.getoutputstream ());
This completes the dynamic graphics verification code generation.
2, the use of graphics code in the page:
Use the tag in the same way that you use other graphics. Assuming the Url-pattern value of the servlet that generates the picture is Checkcode, the code to load the picture on the page is as follows:
Copy Code code as follows:
3. Verify
Gets the authentication code entered by the user, and then obtains the saved verification code from the session and compares it to determine whether it is the same, thus completing the validation.
I hope this article will help you with the JSP program design.