当前位置:网站首页>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
边栏推荐
- Why is the operation unsuccessful (unresolved) uncaught syntaxerror: invalid or unexpected token (resolved)
- What are the advantages and disadvantages of data center agents?
- I don't know why it can't run in the project and how to change it
- After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
- Trading software programming
- Difference between value and placeholder
- Experimental animal models - current market situation and future development trend
- SQL statement
- C import Xls data method summary V (complete code)
- Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
猜你喜欢
Basic editing specifications and variables of shell script
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
Jerry's synchronous weather information to equipment [chapter]
Force buckle day32
A fan summed up so many interview questions for you. There is always one you need!
Maximum likelihood method, likelihood function and log likelihood function
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution
Introduction to Tianchi news recommendation: 4 Characteristic Engineering
Introduction to superresolution
Huawei cloud micro certification Huawei cloud computing service practice has been stable
随机推荐
Write the first CUDA program
Ceramic metal crowns - current market situation and future development trend
The difference between lambda expressions and anonymous inner classes
Conditional statements of shell programming
Solution of cursor thickening
Maximum entropy model
Sequence sorting of basic exercises of test questions
C import Xls data method summary III (processing data in datatable)
Gnupg website
Feign implements dynamic URL
2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
Should enterprises start building progressive web applications?
After listening to the system clear message notification, Jerry informed the device side to delete the message [article]
Do you know the eight signs of a team becoming agile?
C library function int fprintf (file *stream, const char *format,...) Send formatted output to stream
The force deduction method summarizes the single elements in the 540 ordered array
Is Shengang securities company as safe as other securities companies
Remember a lazy query error
求esp32C3板子連接mssql方法
Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales