One. The configuration in Web. XML is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
Id= "webapp_id" version= "3.0" >
<servlet>
<servlet-name>ResponseServlet</servlet-name>
<servlet-class>com.rjxy.model.ResponseServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ResponseServlet</servlet-name>
<url-pattern>/ResponseServlet</url-pattern>
</servlet-mapping>
</web-app>
Two. Create a Responseservlet
Package Com.rjxy.model;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
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;
public class Responseservlet extends HttpServlet {
Private static final long serialversionuid = 1L;
public static final int WIDTH = 120;
public static final int HEIGHT = 35;
protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
BufferedImage image = New BufferedImage (WIDTH, HEIGHT, Bufferedimage.type_int_rgb);
Graphics g = image.getgraphics ();
Set Background color
SetBackground (g);
Set border
SetBorder (g);
Draw interference Lines
Drawrandomline (g);
Write random number
Drawrandownum ((graphics2d) g);
Graphic to Browser
Response.setcontenttype ("Image/jpeg");
Head controller do not cache
Response.setdateheader ("Exprise",-1);
Response.setheader ("Cache-control", "No-cache");
Response.setheader ("Pragma", "No-cache");
Imageio.write (image, "JPG", Response.getoutputstream ());
}
private void Drawrandownum (Graphics2D g) {
Write random characters, the implementation of Chinese characters in Java is Unicode storage, the interval is 0X4E00~0X9FA5
G.setcolor (color.red);
G.setfont (New Font ("Arial", Font.Bold, 20));
String base = "\u7684\u4e00\u5e00\u6e00\u7e00\u8e00\u9e00";
int x = 10;
for (int i = 0; i < 4; i++) {
int degree = new Random (). Nextint ()% 30;//rotation within 30 degree range
String ch = base.charat (new Random (). Nextint (Base.length ())) + "";//The double quotes here are necessary and missing will be treated as char instead of string
G.rotate (Degree * math.pi/180, x);//pi is pi
g.DrawString (CH, x, +);//x has an initial value of 10, indicating that the first word has a horizontal axis of 10 and then executes X+10
G.rotate (-degree * math.pi/180, X, 20);//indicates that the angle of the first turn is zeroed and does not affect subsequent font rotation
x + = 30;
}
}
private void Drawrandomline (Graphics g) {
Draw the interference line, its Gao Xiao at height, width less than width
G.setcolor (Color.green);
for (int i = 0; i < 4; i++) {
int x1 = new Random (). Nextint (WIDTH);
int y1 = new Random (). Nextint (HEIGHT);
int x2 = new Random (). Nextint (WIDTH);
int y2 = new Random (). Nextint (HEIGHT);
G.drawline (x1, y1, x2, y2);
}
}
private void SetBorder (Graphics g) {
Set Border color
G.setcolor (Color.Blue);
G.drawrect (1, 1, WIDTH-2, HEIGHT-2);
}
private void SetBackground (Graphics g) {
Set Background fill Color
G.setcolor (Color.yellow);
G.fillrect (0, 0, WIDTH, HEIGHT);
}
protected void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
Javaweb Browser randomly output a verification code picture