JSP verification code, verification code

Source: Internet
Author: User

JSP verification code, verification code

Write a random 4-digit verification code on the JSP page

Instantiate the BufferedImage class to get the Graphics paint brush to fill the image with the background color and then write a random number and 100 interference points.

Use scropt to refresh the verification code

Question: the DrawString method in videos and books cannot be used and I don't know what's going on. Convert String to char and call another drawChars method.

<%@ page language="java" contentType="text/html; charset=GB2312"import = "java.awt.image.*"import = "java.util.*"import = "java.awt.*"import = "javax.imageio.*"import = "java.lang.*"%>


 


How to write the code that displays the verification code in jsp?

Import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. IOException; import java. io. outputStream; import java. util. random; import javax. imageio. imageIO;/* generate Verification Code image */public class MakeCertPic {// the character set that can appear in the verification code image. You can modify private char mapTable [] = {'A' as needed ', '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'};/* function: generate the color Verification Code image parameter wedth to generate the image width, the parameter height is the height of the generated image. The parameter OS is the output stream of the page */public String getCertPic (int width, int height, OutputStream OS) {if (width <= 0) width = 60; if (height <= 0) height = 20; BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); // obtain the image context Graphics g = image. getGraphics (); // set the background color g. setColor (new Color (0 xDCDCDC); g. fillRect (0, 0, width, height); // draw the border g. setColor (Color. black); g. drawRect (0, 0, width-1, height-1); // The randomly generated verification code String strEnsure = ""; // 4 indicates that 4 is the verification code, to generate more verification codes, increase the value for (int I = 0; I <4; ++ I) {strEnsure + = mapTable [(int) (mapTable. length * Math. random ()];} // display the verification code to the image. to generate more verification codes, add the drawString statement g ....... remaining full text>

Refresh the verification code when using jsp for login

<a href =" "onclick =" document. getElementById ('yanzheng '). src = '.. /admin/yanzheng. jsp? '+ New Date (). getTime () "> cannot see clearly </a>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.