当前位置:网站首页>Servlet simple verification code generation
Servlet simple verification code generation
2022-07-04 01:48:00 【Travel · lost road】
The essence : A picture
Purpose : Prevent malicious form registration
Servlet backstage :
import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet implements Servlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Set the height and width of the verification code image
int width = 100;
int height = 50;
//1. Create an object , Picture in memory ( Captcha image object )
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//2. Beautify the picture
//2.1 Fill background color
Graphics g = image.getGraphics();// Brush object
g.setColor(Color.white);
g.fillRect(0,0,width,height);
//2.2 Picture frame
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// Generate random corners
Random r = new Random();
for (int i = 1; i <= 4; i++) {
int index = r.nextInt(str.length());
// Character acquisition
char ch = str.charAt(index);
//2.3 Write verification code
g.drawString(ch+"",width/5*i,height/2);
}
//2.4 Drew interference
g.setColor(Color.green);
// Randomly generate coordinate points
for (int i = 0; i < 6; i++) {
int x1 = r.nextInt(width);
int x2 = r.nextInt(width);
int y1 = r.nextInt(height);
int y2 = r.nextInt(height);
g.drawLine(x1,y1,x2,y2);
}
//3. Output the picture to the page to show
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}HTML front end :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload = function () {
var img = document.getElementById("checkCode")
img.onclick = function () {
// Time stamp
var date = new Date().getTime();
img.src = "/day15/checkCodeServlet?"+date;
}
}
</script>
</head>
<body>
<img id="checkCode" src="/day15/checkCodeServlet">
<a id="change" href=""> I can't see it clearly. Change it ?</a>
</body>
</html>The effect is as follows :

Its beautification can be modified as needed
边栏推荐
- Hbuilder link Xiaoyao simulator
- Portapack application development tutorial (XVII) nRF24L01 launch C
- Pesticide synergist - current market situation and future development trend
- Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
- Force buckle day32
- C import Xls data method summary IV (upload file de duplication and database data De duplication)
- mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- 2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
- JVM performance tuning and practical basic theory - medium
- What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
猜你喜欢

Force buckle day32

1189. Maximum number of "balloons"

Three layer switching ①

How can enterprises optimize the best cost of cloud computing?

Audio resource settings for U3D resource management

MPLS③

Huawei BFD and NQA

Small program graduation project based on wechat e-book small program graduation project opening report function reference
![[leetcode daily question] a single element in an ordered array](/img/3a/2b465589b70cd6aeec08e79fcf40d4.jpg)
[leetcode daily question] a single element in an ordered array

Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
随机推荐
Difference between value and placeholder
Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.
2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
How can enterprises optimize the best cost of cloud computing?
Luogu p1309 Swiss wheel
Chinese Mitten Crab - current market situation and future development trend
Writeup (real questions and analysis of ciscn over the years) of the preliminary competition of national college students' information security competition
Containerization technology stack
Setting function of Jerry's watch management device [chapter]
【.NET+MQTT】. Net6 environment to achieve mqtt communication, as well as bilateral message subscription and publishing code demonstration of server and client
From the 18th line to the first line, the new story of the network security industry
Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
File contains vulnerability summary
In yolov5, denselayer is used to replace focus, and the FPN structure is changed to bi FPN
Conditional statements of shell programming
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
Neo4j learning notes
Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
Since the "epidemic", we have adhered to the "no closing" of data middle office services