Java image Verification Code

Source: Internet
Author: User

Step 1: Create a Servlet File
// Random Number
Public class generatecode extends httpservlet {
Private Static final long serialversionuid = 5039671097977591965l;

Protected void doget (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Processrequest (req, resp );
}

Protected void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Processrequest (req, resp );
}

Public void processrequest (httpservletrequest request,
Httpservletresponse response) throws ioexception {
Response. setcontenttype ("11.gif"); // set an image
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Httpsession session = request. getsession ();
Int width = 80, Height = 20;
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 (0, 0, width, height );

G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int XL = random. nextint (12 );
Int yl = random. nextint (12 );
G. drawline (X, Y, x + XL, Y + yl );
}

String srand = "";
For (INT I = 0; I <6; I ++ ){
String Rand = string. valueof (random. nextint (10 ));
Srand + = rand;
G. setcolor (new color (20 + random. nextint (110), 20 + random
. Nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}
Session. setattribute ("RAND", srand );
G. Dispose ();
Servletoutputstream responseoutputstream = response. getoutputstream ();
ImageIO. Write (image, "Jpeg", responseoutputstream );
Responseoutputstream. Flush ();
Responseoutputstream. Close ();
}

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 );
}
}

Step 2 set web. xml

<Servlet>
<Servlet-Name> showimgcode </servlet-Name>
<Servlet-class> generatecode </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> showimgcode </servlet-Name>
<URL-pattern>/11.gif</url-pattern>
</Servlet-mapping>

Step 3: Create the corresponding JSP
Index. jsp
<% @ Page contenttype = "text/html; charset = gb2312" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> certificate code input page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
<Meta HTTP-EQUIV = "expires" content = "0">

<Script language = "JavaScript">
Function myrefresh ()
{
Window. Location. Reload ();
}
</SCRIPT>
</Head>
<Body>
<Form method = post action = "check. jsp">
<Table>
<Tr>
<TD align = left>
The verification code generated by the system:
</TD>
<TD>

<Input type = "button" value = "cannot see clearly. Change" onclick = "myrefresh ()">
</TD>
</Tr>
<Tr>
<TD align = left>
Enter the above verification code:
</TD>
<TD>
<Input type = text name = Rand maxlength = 6 value = "">
</TD>
</Tr>
<Tr>
<TD colspan = 2 align = center>
<Input type = submit value = "Submit detection">
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>

Check. jsp
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java"
Errorpage = "" %>
<HTML>
<Head>
<Title> verification page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
<Meta HTTP-EQUIV = "expires" content = "0">
</Head>
<Body>
<%
String Rand = (string) Session. getattribute ("RAND ");
String input = request. getparameter ("RAND ");
%>
The verification code generated by the system is:
<% = Rand %> <br>
The verification code you entered is:
<% = Input %> <br>
<Br>
<%
If (RAND. Equals (input )){
%>
<Font color = green> the input is the same and the authentication is successful! </Font>
<%
} Else {
%>
<Font color = red> If the input is different, authentication fails! </Font>
<%
}
%>
</Body>
</Html>
============================
Verification code for long-lived characters and Data

Public class CharactorCode extends HttpServlet {
Private static final long serialVersionUID = 4347521091342882490L;
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 ("11.gif ");

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 );
}
}
}

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.