The following is the jsp file that calls the class that generates the verification code image:
<%@ page contentType="image/jpeg" import="com.vogoal.util.img.*" %><%response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","no-cache");response.setDateHeader("Expires", 0);RandImgCreater rc = new RandImgCreater(response);//RandImgCreater rc = new RandImgCreater(response,8,"abcdef");//rc.setBgColor(100,100,100);String rand = rc.createRandImage();session.setAttribute("rand",rand);%> |
Put the class compiled by the java program in the directory of Tomcat's web application class and write a test program.
Request page:
<form name="frm" method="post" action="chkImg.jsp">Hello Image Test<br/>checkCode:<img src="img.jsp"><br/>please input the checkCode:<input type="text" name="code" value=""><br/><input type="submit" name="btn1" value="check"></form> |
Verification page:
<%String inputCode = request.getParameter("code");String code = (String)session.getAttribute("rand");if ( inputCode.equals(code) ){%>check SUCCESS!!!!!<%}else{%>wrong code!!!!!!!<%}%> |
Place all jsp files in the directory of your web application.
Start test:
Start Tomcat and access the request page.
Enter the verification code displayed in the image and click "check". The following figure is displayed. The test is successful.
At this point, the image Verification Code of jsp is basically implemented. In reality, we can use java's image class to implement more functions. If necessary, we will continue to discuss implementation methods.
Help:
The attachment provides the image generation class and jsp file to test the download of jsp files.
You can modify the img. jsp file to meet your functional requirements.
1. Constructor
RandImgCreater provides two constructors.
The default constructor needs to set the response object.
Public RandImgCreater (HttpServletResponse response)
When using this constructor, a four-digit Verification Code image is generated. The verification code is a random combination of uppercase and lowercase English letters and numbers.
Public RandImgCreater (HttpServletResponse response, int iNum, String codeList)
When using this constructor, iNum is the number of digits of the displayed verification code, and codeList is the generated metacharacter of the Verification Code specified by the user. (Chinese characters are not supported)
Example
RandImgCreater rc = new RandImgCreater (response, 8, "abcdef ");
An 8-digit Verification Code image randomly retrieved from abcdef is generated.
2. Specify the background color
Public void setBgColor (int r, int g, int B)
This method is optional. When this parameter is set, the image background is displayed based on the user-specified background color. If this parameter is not set, the default background is used.
R, g, and B are the set values of each color in the RGB color. Value Range: 0-255
Example
Rc. setBgColor (100,100,100 );
Source code download: http://www.webjx.com/files/soft/imageChecker.zip