1, login.jsp page program
Copy Code code as follows:
<script type= "Text/javascript" >
function Changevalidatecode (obj) {
Get the current time as a parameter, no specific meaning
var timenow = new Date (). GetTime ();
Each request requires a different parameter, or the same verification code may be returned
This has to do with the browser's caching mechanism, or you can set the page to not cache, so you don't need this parameter.
Obj.src= "rand.action?d=" +timenow;
}
</script>
Add the following sentence to the form:
Copy Code code as follows:
<s:text name= "random" ></s:text>:<s:textfield name= "Rand" size= "5" ></s:textfield>
2, Randomnumutil.java generation of authentication code class file
Copy Code code as follows:
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
Import Javax.imageio.stream.ImageOutputStream;
public class Randomnumutil {
Private Bytearrayinputstream image;//Image
Private String str;//Authentication code
Private Randomnumutil () {
Init ();//Initialize Property
}
/*
* Obtain Randomnumutil instance
*/
public static Randomnumutil Instance () {
return new Randomnumutil ();
}
/*
* Get the verification code picture
*/
Public Bytearrayinputstream GetImage () {
return this.image;
}
/*
* Get the verification code of the picture
*/
Public String getString () {
return this.str;
}
private void init () {
Create an image in memory
int width=85, 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));
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<155;i++)
{
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.drawline (X,y,x+xl,y+yl);
}
Take a randomly generated authentication code (6 digits)
String srand= "";
for (int i=0;i<6;i++) {
String rand=string.valueof (Random.nextint (10));
Srand+=rand;
Display the authentication code in the image
G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
The call function comes out with the same color, possibly because the seed is too close, so it can only be generated directly
g.DrawString (rand,13*i+6,16);
}
Assignment Verification Code
This.str=srand;
Image entry into force
G.dispose ();
Bytearrayinputstream Input=null;
Bytearrayoutputstream output = new Bytearrayoutputstream ();
try{
Imageoutputstream imageout = imageio.createimageoutputstream (output);
Imageio.write (Image, "JPEG", imageout);
Imageout.close ();
input = new Bytearrayinputstream (Output.tobytearray ());
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Validation code picture produces error:" +e.tostring ());
}
this.image=input;/* Assignment Image * *
}
/*
* Get random colors for a given range
*/
Private Color getrandcolor (int fc,int BC) {
Random Random = new Random ();
if (fc>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);
}
}
3, Randomaction.java generate Verification Code action Program
Copy Code code as follows:
Import Java.io.ByteArrayInputStream;
Import Com.mxl.util.RandomNumUtil;
Import Com.opensymphony.xwork2.ActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Randomaction extends actionsupport{
Private Bytearrayinputstream InputStream;
Public String Execute () throws exception{
Randomnumutil rdnu=randomnumutil.instance ();
This.setinputstream (Rdnu.getimage ());//Get a picture with a random string
Actioncontext.getcontext (). GetSession (). Put ("random", rdnu.getstring ());//Get random string into HttpSession
return SUCCESS;
}
public void Setinputstream (Bytearrayinputstream inputstream) {
This.inputstream = InputStream;
}
Public Bytearrayinputstream getInputStream () {
return inputstream;
}
}
4, Loginaction.java Verification Code of the action
Copy Code code as follows:
Private String Rand; Rand in the form
Public String Getrand () {
return to Rand;
}
public void Setrand (String rand) {
This.rand = rand;
}
Remove the validation code generated in the Randomaction.java from the session random
String arandom= (String) (Actioncontext.getcontext (). GetSession (). Get ("random");
The following is a comparison of the validation code string stored in the session with the customer-entered verification code string.
if (Arandom.equals (This.getrand ())) {
Actioncontext.getcontext (). GetSession (). Put ("User", This.getusername ());
return SUCCESS;
}
else {
return ERROR;
}
5. Configure Struts.xml Files
Copy Code code as follows:
<!--random Verification code-->
<action name= "Rand" class= "Com.mxl.rand.RandomAction" >
<result type= "Stream" >
<param name= "ContentType" >image/jpeg</param>
<param name= "InputName" >inputStream</param>
</result>
</action>
6, generated verification Code picture Demo (Implementation of the 6-digit verification code)
Description
If you want to modify the number of validation code generation, you need to modify the following places:
1th:
int width=85, height=20; int width=85, height=20;
2nd: for (int i=0;i<6;i++) for (int i=0;i<6;i++)
Number 6, change to the number of digits you want to generate ~