Java Verification Code

Source: Internet
Author: User
Recently, a verification code is used in a J2EE system. The following is a good method to write the verification code as a servlet.

Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io .*;
Import java. AWT .*;
Import java. AWT. image .*;
Import java. util .*;
Import javax. ImageIO .*;

/**
* @ Author yeeku. H. Lee kongyeeku@163.com
* @ Version 1.0
* <Br> copyright (C), 2005-2008, yeeku. H. Lee
* <Br> this program is protected by copyright laws.
* <Br> program name:
* <Br> date:
*/
Public class authimg extends httpservlet
{
Private font mfont = new font ("Arial black", Font. Plain, 16 );
Public void Init () throws servletexception
{
Super. INIT ();
}
Color getrandcolor (int fc, int BC)
{
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 );
}

Public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. setcontenttype ("image/JPEG ");

Int width = 100, Height = 18;
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );

Graphics G = image. getgraphics ();
Random random = new random ();
G. setcolor (getrandcolor (200,250 ));
G. fillrect (1, 1, width-1, height-1 );
G. setcolor (new color (102,102,102 ));
G. drawrect (0, 0, width-1, height-1 );
G. setfont (mfont );

G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++)
{
Int x = random. nextint (width-1 );
Int y = random. nextint (height-1 );
Int XL = random. nextint (6) + 1;
Int yl = random. nextint (12) + 1;
G. drawline (X, Y, x + XL, Y + yl );
}
For (INT I = 0; I <70; I ++)
{
Int x = random. nextint (width-1 );
Int y = random. nextint (height-1 );
Int XL = random. nextint (12) + 1;
Int yl = random. nextint (6) + 1;
G. drawline (X, Y, X-XL, Y-yl );
}

String srand = "";
For (INT I = 0; I <6; I ++)
{
String TMP = getrandomchar ();
Srand + = TMP;
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (TMP, 15 * I + 10, 15 );
}

Httpsession session = request. getsession (true );
Session. setattribute ("RAND", srand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
}
Private string getrandomchar ()
{
Int Rand = (INT) math. Round (math. Random () * 2 );
Long itmp = 0;
Char CTMP = '\ u0000 ';
Switch (RAND)
{
Case 1:
Itmp = math. Round (math. Random () * 25 + 65 );
CTMP = (char) itmp;
Return string. valueof (CTMP );
Case 2:
Itmp = math. Round (math. Random () * 25 + 97 );
CTMP = (char) itmp;
Return string. valueof (CTMP );
Default:
Itmp = math. Round (math. Random () * 9 );
Return string. valueof (itmp );
}
}
}
Here, the interference line is added and the verification code is saved using session,
On the login JSP page, Set
The Verification Code cannot be viewed clearly? <A href = "#" onclick = "Refresh ()"> click here to refresh </a>
Then write the refesh () function as follows:
<SCRIPT>
Function refresh ()
{
Document. getelementbyid ("authimg"). src = 'authimg? Now = '+ new date ();
}
</SCRIPT>
Adding now at the end prevents browser caching.

Finally, configure the servlet in Web. xml.

<Servlet>
<Servlet-Name> IMG </servlet-Name>
<Servlet-class> org. yeeku. Web. authimg </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> IMG </servlet-Name>
<URL-pattern>/authimg </url-pattern>
</Servlet-mapping>

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.