Complete jsp page Verification Code instance, jsp page Verification Code

Source: Internet
Author: User

Complete jsp page Verification Code instance, jsp page Verification Code

This example shares the code of the sp page verification code for your reference. The details are as follows:

The Project structure is as follows. Create a new Web Project in MyEclipse and name it servlet.

1. A new servlet class under src

Package com. servlet; import java. awt. color; import java. awt. font; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. IOException; import java. io. printWriter; import java. util. random; import javax. servlet. servletException; import javax. servlet. servletOutputStream; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. required imageencoder; public class IdentityServlet extends HttpServlet {public static final char [] chars = {'2', '3', '4', '5', '6 ', '7', '8', '9', 'A'}; // custom Verification Code pool public static Random random = new Random (); // random number public static String getRandomString () {// obtain a random number of 6 digits. Put StringBuffer buffer = new StringBuffer (); for (int I = 0; I <6; I ++) {buffer. append (chars [random. nextInt (chars. length)]);} return buffer. toString ();} public static Color getRandomColor () {// get random Color return new Color (random. nextInt (255), random. nextInt (255), random. nextInt (255);} public static Color getReverseColor (Color c) {// returns the reversed Color of a Color return new Color (255-c. getRed (), 255-c. getGreen (), 255-c. getBlue ();}/*** Constructor of the object. */public IdentityServlet () {super ();}/*** Destruction of the servlet. <br> */public void destroy () {super. destroy (); // Just puts "destroy" string in log // Put your code here}/*** The doGet method of the servlet. <br> ** This method is called when a form has its tag value method equals to get. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doGet (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {response. setContentType ("image/jpeg"); // set the output type String randomString = getRandomString (); // random String request. getSession (true ). setAttribute ("randomString", randomString); // put int width = 100 in the session; // The Image width int height = 30; // The Image height Color = getRandomColor (); // random Color, used for background color reverse = getReverseColor (Color); // reversed color, used for foreground Color // create a color image BufferedImage bi = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2D g = bi. createGraphics (); // Drawing Object g. setFont (new Font (Font. SANS_SERIF, Font. BOLD, 16); // set the font g. setColor (color); // set the color g. fillRect (0, 0, width, height); // draw the background g. setColor (reverse); g. drawString (randomString, 18, 20); // draw random characters for (int I = 0, n = random. nextInt (100); I <n; I ++) {// draw a maximum of 100 noise points g. drawRect (random. nextInt (width), random. nextInt (height), 1, 1);} ServletOutputStream out = response. getOutputStream (); // convert it to JPEG format: Required imageencoder encoder = required codec. createJPEGEncoder (out); // encoder. encode (bi); // encode the image out. flush (); // output to The client}/*** the doPost method of The servlet. <br> ** This method is called when a form has its tag value method equals to post. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {doGet (request, response);}/*** Initialization of the servlet. <br> ** @ throws ServletException if an error occurs */public void init () throws ServletException {// Put your code here }}

2. web. xml automatically generates servlet and servlet-mapping configurations.

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"   xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet>  <servlet-name>IdentityServlet</servlet-name>  <servlet-class>com.servlet.IdentityServlet</servlet-class> </servlet> <servlet-mapping>  <servlet-name>IdentityServlet</servlet-name>  <url-pattern>/servlet/IdentityServlet</url-pattern> </servlet-mapping>   <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>

3. Create an html file under WebRoot to display the verification code.

<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Start Tomcat and enter the URL http: // localhost: 8080/servlet/identity.html. The effect is as follows:

Click Change image to generate a new verification code.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.