Use a JSP file to generate a website Verification Code

Source: Internet
Author: User

Random. jsp (preferred for SUN Enterprise Applications) file code:

<% @ Page autoFlush = "false" import = "java. awt. *, java. awt. image. *, com.sun.image.codec.jpeg. *, java. util. *" %>
<% @ Page import = "org. apache (the most popular WEB server platform on Unix). 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 (preferred for SUN Enterprise Applications) 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 (the most popular WEB server platform on Unix) packages, You can compile the following file by yourself.
// FrontEnd Plus GUI for JAD
// DeCompiled: RandomStringUtils. class

Package org. apache (the most popular WEB server platform on the Unix platform). 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, 32,127, false, false );

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.