Generate verification code in Servlet

Source: Internet
Author: User

Generate verification code in Servlet

Package login;

Import java. AWT. color;
Import java. AWT. graphics;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. Io. outputstream;
Import java. Io. printwriter;
Import java. util. Random;


Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;


Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
/**
* Generate a verification code Servlet
* @ Author Administrator
*
*/
Public class checkcodeservlet extends httpservlet {
Public void Service (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
System. Out. println ("service called ");
/** 1. Set the server response type to JPG */
Response. setcontenttype ("image/JPEG ");
/** 2. Drawing **/
// 1. Create a space to store images in the memory
Bufferedimage image = new bufferedimage (60, 25, bufferedimage. type_int_rgb );
// 2. Get the paint brush
Graphics G = image. getgraphics ();
// 3. Prepare a random number object
Random r = new random ();
// 4. Set the background color of the image
G. setcolor (new color (R. nextint (255), R. nextint (255), R. nextint (255 )));
// 5. Fill
G. fillrect (0, 0, 60, 25 );
// 6. Set the foreground color of the paint brush (set the font color)
G. setcolor (new color (0, 0 ));
// 7. Generate a random number
String number = string. valueof (R. nextint (99999) + 1000 );
/** Store the verification code in the session */
Httpsession session = request. getsession ();
Session. setattribute ("checkcode", number );
// 8. Digital Image
G. drawstring (number, 15, 15 );
G. drawline (R. nextint (20), R. nextint (20), R. nextint (50), R. nextint (50 ));
G. drawline (R. nextint (20), R. nextint (20), R. nextint (50), R. nextint (50 ));
G. drawline (R. nextint (20), R. nextint (20), R. nextint (50), R. nextint (50 ));
G. drawline (R. nextint (20), R. nextint (20), R. nextint (50), R. nextint (50 ));
/** 3. Use a specific format to compress the image and output it */
// 1. Create a file output stream
Outputstream OS = response. getoutputstream ();
// 2. Use the specified format
Required imageencoder encoder = required codec. createjpegencoder (OS );
Encoder. encode (image );

}


}


Use the verification code generated by Java on the JSP page:

<TD style = "width: 80px">
Verification Code:
</TD>
<TD valign = "Middle">
<Input name = "validatecode" size = "7px"/>
<! -- Equivalent to "#" -->
<A href = "javascript:;" onclick = "document. getelementbyid ('img ').
Src = 'checkcode? '+ New date (). gettime (); "> <span style =" font-size: 12px; "> can't see clearly, change one </span> </a>
<Br/>
<Span style = "color: red;">
<%
String check_error = (string) request. getattribute ("check_error ");
%>
<% = Check_error = NULL? "": Check_error %>
</Span>
</TD>



Configuration in the xmd File

<Servlet>
<Servlet-Name> check </servlet-Name>
<Servlet-class> login. checkcodeservlet </servlet-class>
</Servlet>


<Servlet-mapping>
<Servlet-Name> check </servlet-Name>
<URL-pattern>/checkcode </url-pattern>
</Servlet-mapping>

Some code for verification and judgment:

String validatecode = request. getparameter ("validatecode"); // Verification Code

String checkcode = (string) Session. getattribute ("checkcode ");

Compare In Servlet














































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.