Use a JSP file to generate a website Verification Code

Source: Internet
Author: User

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 ();
// Fill in the following 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 ();
Required imageencoder encoder = required codec. createjpegencoder (outstream );
Encoder. encode (image );
Outstream. Close ();
%>

The code for the test. jsp file is as follows:
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<HTML>
<Head>
<Title> untitled document </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>

<Body>

</Body>
</Html>

If you do not want to import Apache packages, compile the following file by 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 = new random ();

Public randomstringutils ()
{
}

Public static string random (INT count)
{
Return random (count, false, false );
}

Public static string randomascii (INT count)
{
Return random (count, 32,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.