Detailed explanation of image Verification Code Generation Technology and JSP image verification code generation in jsp development
Detailed explanation of image Verification Code Generation Technology in JSP development
When registering users on a webpage, we often need to enter the verification code according to the image verification code given by the image. So we will learn this today.
To put it simply, there are three steps:
1. The underlying layer uses Java to generate Verification Code Images
2. Use the configuration file call to implement Java generation.
3. display images to webpages using HTML technology
First, the bottom layer Java generates Image Code
Package cn. hncu. servlets; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. fileOutputStream; import java. io. IOException; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletRe Extends se; public class CodeImgServlet extends HttpServlet {@ Override protected void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// ☆1 ☆-- resp. setContentType ("image/jpeg"); // set the http Response Header --- tell the browser that I am sending data in this image format, you can use the corresponding method to parse // define the width and height of the image. int w = 60; int h = 30; // declare the image BufferedImage img = new BufferedImage (w, h, BufferedImage. TYPE_INT_R GB); Graphics g = img. getGraphics (); // Changes the background to white g. setColor (Color. white); g. fillRect (0, 0, w, h); // set the font g. setFont (new Font ("aa", Font. BOLD, 18); // generate and draw four Random numbers Random r = new Random (); for (int I = 0; I <4; I ++) {int a = r. nextInt (10); // generate 0 ~ Random integer between 9 int y = 15 + r. nextInt (20); // generate a random vertical position // generate a random Color c = new Color (r. nextInt (1, 256), r. nextInt (1, 256), r. nextInt (256); g. setColor (c); g. drawString ("" + a, I * 15, y);} // draw several interference lines for (int I = 0; I <10; I ++) {// generate random Color c = new Color (r. nextInt (1, 256), r. nextInt (1, 256), r. nextInt (256); g. setColor (c); g. drawLine (r. nextInt (60), r. nextInt (30), r. nextInt (60), r. nextInt (30);} g. dispose (); // similar to flush () in IO, flush graphic data to img // Save the img object in memory to a jpg file ImageIO. write (img, "JPEG", resp. getOutputStream (); // ☆2 ☆ }}
Code of the Web. xml configuration file in the project
<? 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> <! -- If you want to allow external access to jsp files in the security directory. xml to configure it into a jsp-servlet --> <servlet-name> ajsp </servlet-name> <! -- Configure servlet with servlet-class, configure jsp page with jsp-file tag --> <jsp-file>/WEB-INF/jsps/. jsp </jsp-file> </servlet> <! -- <Servlet> <servlet-name> CodeImgServlet </servlet-name> <servlet-class> cn. hncu. servlets. codeImgServlet </servlet-class> </servlet> <servlet-name> LoginServlet </servlet-name> <servlet-class> cn. hncu. servlets. loginServlet </servlet-class> </servlet> --> <servlet-name> CodeImgServlet </servlet-name> <servlet-class> cn. hncu. servlets. codeImgServlet </servlet-class> </servlet> <ser Vlet-name> LoginServlet </servlet-name> <servlet-class> cn. hncu. servlets. LoginServlet </servlet-class> </servlet> <! -- Configure the access path for the servlet --> <servlet-mapping> <servlet-name> ajsp </servlet-name> <url-pattern>/x/. asp </url-pattern> </servlet-mapping> <servlet-name> CodeImgServlet </servlet-name> <url-pattern>/code </url -pattern> </servlet-mapping> <servlet-name> LoginServlet </servlet-name> <url-pattern>/LoginServlet </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> index. jsp </welcome-file> </welcome-file-list> </web-app>
Homepage code logon code
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
When a page is loaded, it requests the service from TOMCAT, calls the previous JAVA class, generates an image, and loads the image to the page through the HTML document.
Second, the Web. xml configuration file can also be used to set access to secure directory files (WEB-INF)
<Servlet> <servlet-name> ajsp </servlet-name> <! -- Configure servlet with servlet-class, configure jsp page with jsp-file tag --> <jsp-file>/WEB-INF/jsps/. jsp </jsp-file> </servlet> <servlet-name> CodeImgServlet </servlet-name> <servlet-class> cn. hncu. servlets. codeImgServlet </servlet-class> </servlet> <servlet-name> LoginServlet </servlet-name> <servlet-class> cn. hncu. servlets. loginServlet </servlet-class> </servlet> <! -- Configure the access path for the servlet --> <servlet-mapping> <servlet-name> ajsp </servlet-name> <url-pattern>/x/. asp </url-pattern> </servlet-mapping> <servlet-name> CodeImgServlet </servlet-name> <url-pattern>/code </url -pattern> </servlet-mapping> <servlet-name> LoginServlet </servlet-name> <url-pattern>/LoginServlet </url-pattern> </servlet-mapping>
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!