Example of Generating Random verification code using a jsp page

Source: Internet
Author: User

CheckNum. jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "UTF-8" %>
<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, javax. imageio. *" %>

<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>

<%!
Color getRandColor (int fc, int bc) // random Color selection within a given range
{
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 );
}
%>
<%
Response. setHeader ("Pragma", "No-cache"); // set the page to not Buffer
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
Int width = 60, height = 20;
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
Graphics g = image. getGraphics (); // obtain the image Context
Random random = new Random (); // generates Random objects.
G. setColor (getRandColor (200,250 ));
G. fillRect (0, 0, width, height );
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18); // set the Font
For (int I = 0; I <155; I ++)
{
Int x = random. nextInt (width );
Int y = random. nextInt (height );
Int x1 = random. nextInt (12 );
Int y1 = random. nextInt (12 );
G. drawLine (x, y, x + x1, y + y1 );
}
// Generate a random Verification Code
String sRand = "";
For (int I = 0; I <4; I ++)
{
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
// Display the verification code to the image
G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 )));
// Set the color of the random number.
G. drawString (rand, 13 * I + 6, 16 );
}
// Store the verification code in the session
Session. setAttribute ("rand", sRand );
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
Out. clear ();
Out = pageContext. pushBody ();

%>

You can log on to login. jsp using the following method;
Verification Code: <input class = "imgbutton" name = "checknum" type = "text" size = "15" id = "checknum" maxlength = "4"/>
<br/>
<Input type = "submit" name = "submit" value = "login"/>

Related Article

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.