Operation steps of generating random number verification code by SSH


1、 login.jsp Page program

Copy codeThe code is as follows:
<script type=”text/javascript”>
function changeValidateCode(obj) {
//Get the current time as a parameter, no specific significance
var timenow = new Date().getTime();
//Each request requires a different parameter, otherwise the same verification code may be returned
//This has something to do with the browser’s caching mechanism. You can also set the page not to cache, so that you don’t need this parameter.

Add the following sentence to the form:

Copy codeThe code is as follows:
<s:text name=”random”></s:text>:<s:textfield name=”rand” size=”5″></s:textfield><img src=” rand.action “Onclick =” changevalidatecode (this) “title =” click on the image to refresh the verification code “/ >”

2、 RandomNumUtil.java Class file for generating captcha

Copy codeThe code is 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; / / verification code
private RandomNumUtil(){
Init(); / / initialize properties
*Get randomnumutil instance
public static RandomNumUtil Instance(){
return new RandomNumUtil();
*Get captcha image
public ByteArrayInputStream getImage(){
return this.image;
*Get the captcha of the picture
public String getString(){
return this.str;
private void init() {
//Creating images in memory
int width=85, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//Get graphic context
Graphics g = image.getGraphics();
//Generating random classes
Random random = new Random();
//Set background color
g.fillRect(0, 0, width, height);
//Set font
g.setFont(new Font(“Times New Roman”,Font.PLAIN,18));
//155 interference lines are generated randomly, so that the authentication code in the image is not easy to be detected by other programs
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);
//Take randomly generated authentication code (6 digits)
String sRand=””;
for (int i=0;i<6;i++){
String rand=String.valueOf(random.nextInt(10));
//Display the authentication code in the image
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//The color of the call function is the same, probably because the seed is too close, so it can only be generated directly
//Assignment verification code
//Image validation
ByteArrayInputStream input=null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, “JPEG”, imageOut);
input = new ByteArrayInputStream(output.toByteArray());
}catch(Exception e){
System.out.println (“error in captcha image generation: + e.tostring());
this.image=input ; / * assignment image*/
*Get random color in 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 Action program for generating verification code

Copy codeThe code is 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 the random string and put it into httpsession
return SUCCESS;
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
public ByteArrayInputStream getInputStream() {
return inputStream;

4、 LoginAction.java Action of verification code

Copy codeThe code is as follows:
Private string Rand; / / Rand in the form
public String getRand() {
return rand;
public void setRand(String rand) {
this.rand = rand;
//Remove from session RandomAction.java The verification code random generated in
String arandom=(String)(ActionContext.getContext().getSession().get(“random”));
//The following is to compare the captcha string saved in session with the captcha string entered by the customer
if(arandom.equals(this.getRand())) {
ActionContext.getContext().getSession().put(“user”, this.getUsername());
return SUCCESS;
else {
return ERROR;

5. Configuration struts.xml file

Copy codeThe code is as follows:
<! — random captcha — >
<action name=”rand”>
<result type=”stream”>
<param name=”contentType”>image/jpeg</param>
<param name=”inputName”>inputStream</param>

6. Generated captcha image demo (6-digit captcha)
If you want to modify the number of generated captcha, you need to modify the following:
First point:
int width=85, height=20; int width=85, height=20;
Second point: for (int i = 0; I < 6; I + +) for (int i = 0; I < 6; I + +)
Change the number 6 to the number you want to generate

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]