Using JSP page to generate random verification Digital code sample _JSP programming

Source: Internet
Author: User
Tags rand
checknum.jsp
Copy Code code 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)//given range randomly selected colors
{
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 Page not buffered
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 ();//Get Image context
Random Random = new Random ();//Generate Random Object
G.setcolor (Getrandcolor (200,250));
G.fillrect (0,0,width,height);
G.setfont (New Font ("Times New Roman", font.plain,18);/Set 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);
}
Random generation of verification code
String SRand = "";
for (int i=0;i<4;i++)
{
String rand = string.valueof (Random.nextint (10));
Srand+=rand;
Display the captcha in the image
G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
Set the color to display random numbers
g.DrawString (rand,13*i+6,16);
}
Store the CAPTCHA in session
Session.setattribute ("Rand", SRand);
Image effective
G.dispose ();
Output image to Page
Imageio.write (Image, "JPEG", Response.getoutputstream ());
Out.clear ();
out = Pagecontext.pushbody ();

%>

Can be passed directly under the login login.jsp: this format call;
Verification code: <input class= "Imgbutton" name= "Checknum" type= "text" size= "" "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.