The examples in this article share the Java image verification code for your reference. The specific content is as follows
Web page display effect:
index.jsp
Use two ways to force image updates:
1. Set the image output without cache.
2. Use js to change the requested address.
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html> <head> <title>Homepage of the first web project</title> <script type="text/javascript"> function changeImg(){ var _img=document.getElementById("_img"); var time=new Date().getTime();//The browser believes that the same action (filtered) does not need to answer, so it uses the time value to change the src value (i.e. the request) (in fact, the same request) _img.src="confirm?"+time;//Change the request address with js} </script> </head> <body> <h2>Homepage</h2> <h2>Changsha, Hunan</h2> <h3>Publish method 1....</h3> <img src="images/1.jpg"/> <br/> <a href="jsps/a.jsp">Go to subpage</a> <hr/> <form > Name:<input type="text" name="nmae"/><br/> Password:<input type="password" name="pwd"/> <br/> Confirm password:<input type="text" name=pwd2/> <br/> Verification code:<input type="text" /><img id="_img" src="confirm"/> <!-- Not feasible<a href="confirm">cannot see clearly</a> <br/> --> <a href="javascript:changeImg();">cannot see clearly</a> <br/> <input type="submit" value="register"/> </form> </body></html> Java code that automatically generates verification code
Key categories (the following classes are the contents of the basic part)
BufferedImage
ImageIO
Graphics/Graphics2D
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;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.HttpServletResponse;public class ImageServlet extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// System.out.println("jinlaile"); resp.setContentType("image/jpeg");//jpeg is the image format. Set the image of the type of response content to jpeg int width=64; int height=40; BufferedImage bImg=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g=bImg.getGraphics(); //Background g.setColor(Color.white); g.fillRect(0, 0, width, height); //Font color g.setFont(new Font("aa", Font.BOLD,18)); //Use random numbers to generate verification code: 4 integers within 0~9 Random r=new Random(); for(int i=0;i<=4;i++){ int t=r.nextInt(10);//Random integers within 10 int y=10+r.nextInt(20);//Up and down positions: 10~30 Color c=new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g.setColor(c); g.drawString(""+t, i*16, y); } //Draw interference lines for(int i=1;i<8;i++){ Color c=new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g.setColor(c); g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } //Flash the graph into the bImg object g.dispose(); //Equivalent to the close() method in IO with automatic flush(); ImageIO.write(bImg,"JPEG", resp.getOutputStream()); //Get the outputStream object of req through resp, send it to the client socket encapsulation, that is, write to the client}}web.xml
<?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> <!-- Access the b.jsp file in the secure directory WEB-INF--> <servlet> <servlet-name>bbbb</servlet-name> <jsp-file>/WEB-INF/b.jsp</jsp-file> </servlet> <!-- Access the java code that generates the verification code--> <servlet> <servlet-name>img</servlet-name> <servlet-class>cn.hncu.servlet.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>bbbb</servlet-name> <url-pattern>/bb.asp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>img</servlet-name> <url-pattern>/confirm</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>