Servlet (7) generate Verification Code
1. ImageCode. java
Package com. hunhun. utils; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. IOException; import java. io. outputStream; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** Servlet implementation class ImageCode */@ WebServlet ("/ImageCode") public class ImageCode extends HttpServlet {private static final long serialVersionUID = 1L; /*** @ see HttpServlet # HttpServlet () */public ImageCode () {super (); // TODO Auto-generated constructor stub}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {// TODO Auto-generated method stub // disable the browser from caching random image response. setDateHeader ("Expires",-1); response. setHeader ("Cache-Control", "no-cache"); response. setHeader ("Pragma", "no-cache"); // notify the browser to display the data response as an image. setHeader ("Content-Type", "image/png"); // create an image in the memory BufferedImage image = new BufferedImage (, BufferedImage. TYPE_INT_RGB); // obtain the image context Graphics g = image. getGraphics (); // set the background color g. setColor (Color. black); g. fillRect (0, 0, 75, 20); // generates a Random class Random r = new Random (); // generates a four-digit verification code String srcChars = "Random "; string number = ""; for (int I = 0; I <4; I ++) {number + = srcChars. charAt (r. nextInt (srcChars. length ();} // set the font and color g. setFont (new Font ("Arial", Font. BOLD | Font. ITALIC, 16); // new Font (name, style, size) g. setColor (new Color (r. nextInt (1, 255), r. nextInt (1, 255), r. nextInt (255); // randomly generates 15 interference lines, making the authentication code in the image hard to be detected by other programs for (int I = 0; I <15; I ++) {int x = r. nextInt (75); int y = r. nextInt (20); int xl = r. nextInt (12); int yl = r. nextInt (12); g. drawLine (x, y, x + xl, y + yl);} g. setColor (new Color (r. nextInt (1, 255), r. nextInt (1, 255), r. nextInt (255); g. drawString (number, 10, 15); // Save the session request. getSession (). setAttribute ("code", number); OutputStream OS = response. getOutputStream (); ImageIO. write (image, "png", OS); // your imageencoder encoder = Your codec. createJPEGEncoder (OS); // encoder. encode (image);}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis. doGet (request, response );}}
2. login. jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
Insert title here
Image style: