Generate site verification code with JSP file

Source: Internet
Author: User
Tags count stringbuffer
js| Authentication Code random.jsp file code:

<%@ page autoflush= "false" import= "java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>
<%@ page import= "Org.apache.commons.lang.RandomStringUtils"%>
<%
Randomstringutils rs=new randomstringutils ();
String Random=rs.randomalphanumeric (4);
Session.setattribute ("random", random);
%>
<%

Out.clear ();
Response.setcontenttype ("Image/jpeg");
Response.AddHeader ("Pragma", "no-cache");
Response.AddHeader ("Cache-control", "No-cache");
Response.adddateheader ("Expries", 0);
int width=100, height=40;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);
Graphics g = image.getgraphics ();
The following fill background color
G.setcolor (Color.gray);
Font defont=new font ("Sansserif", Font.plain, 32);
G.setfont (Defont);
G.fillrect (0, 0, width, height);
Font Color
G.setcolor (color.red);
g.DrawString (random,3,30);
G.dispose ();
Servletoutputstream OutStream = Response.getoutputstream ();
JPEGImageEncoder encoder =jpegcodec.createjpegencoder (OutStream);
Encoder.encode (image);
Outstream.close ();
%>

The test.jsp file code is as follows:
<%@ page contenttype= "text/html; Charset=utf-8 "language=" java "import=" java.sql.* "errorpage=" "%>"
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>

</body>

If you don't want to import Apache packages, you can compile the following file yourself
Frontend Plus GUI for JAD
DeCompiled:RandomStringUtils.class

Package Org.apache.commons.lang;

Import Java.util.Random;

public class Randomstringutils
{

private static final Random Random = new Random ();

Public Randomstringutils ()
{
}

public static String random (int count)
{
return random (count, false, false);
}

public static String randomascii (int count)
{
return random (count, 127, false, false);
}

public static String randomalphabetic (int count)
{
return random (count, true, false);
}

public static String randomalphanumeric (int count)
{
return random (count, true, true);
}

public static String randomnumeric (int count)
{
return random (count, false, true);
}

public static String Random (int count, Boolean letters, Boolean numbers)
{
return random (count, 0, 0, letters, numbers);
}

public static String Random (int count, int start, int end, Boolean letters, Boolean numbers)
{
return random (count, start, end, letters, numbers, NULL);
}

public static String Random (int count, int start, int end, Boolean letters, Boolean numbers, char set[])
{
if (start = = 0 && end = 0)
{
end = 122;
start = 32;
if (!letters &&!numbers)
{
start = 0;
end = 0x7fffffff;
}
}
StringBuffer buffer = new StringBuffer ();
int gap = End-start;
while (count--! = 0)
{
Char ch;
if (set = = null)
ch = (char) (Random.nextint (GAP) + start);
Else
ch = set[random.nextint (GAP) + start];
if (Letters && numbers && character.isletterordigit (ch) | | letters && character.isletter (CH) | | Numbers && character.isdigit (ch) | | !letters &&!numbers)
Buffer.append (CH);
Else
count++;
}
return buffer.tostring ();
}

public static String Random (int count, String set)
{
return random (Count, Set.tochararray ());
}

public static String Random (int count, Char set[])
{
return random (count, 0, Set.length-1, False, false, set);
}

}




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.