当前位置:网站首页>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
边栏推荐
- Notice on Soliciting Opinions on the draft of information security technology mobile Internet application (APP) life cycle security management guide
- MySQL utilise la vue pour signaler les erreurs, Explicit / show ne peut pas être publié; Verrouillage des fichiers privés pour la table sous - jacente
- Lightweight Pyramid Networks for Image Deraining
- Solution to the problem that jsp language cannot be recognized in idea
- MySQL introduction - functions (various function statistics, exercises, details, tables)
- 51 single chip microcomputer timer 2 is used as serial port
- 2022 new examination questions for safety management personnel of hazardous chemical business units and certificate examination for safety management personnel of hazardous chemical business units
- Life cycle of instance variables, static variables and local variables
- Which insurance products can the elderly buy?
- C import Xls data method summary IV (upload file de duplication and database data De duplication)
猜你喜欢
Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
Small program graduation project based on wechat reservation small program graduation project opening report reference
Hbuilder link Xiaoyao simulator
Force buckle day32
0 basic learning C language - nixie tube dynamic scanning display
Will the memory of ParticleSystem be affected by maxparticles
Introduction to Tianchi news recommendation: 4 Characteristic Engineering
MPLS③
Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge
Three layer switching ②
随机推荐
All ceramic crowns - current market situation and future development trend
Feign implements dynamic URL
Douban scoring applet Part-3
How to delete MySQL components using xshell7?
Magical usage of edge browser (highly recommended by program ape and student party)
Special copy UML notes
Flex flexible layout, box in the middle of the page
Pyinstaller packaging py script warning:lib not found and other related issues
In yolov5, denselayer is used to replace focus, and the FPN structure is changed to bi FPN
Jerry's update contact [article]
Portable two-way radio equipment - current market situation and future development trend
A little understanding of GSLB (global server load balance) technology
Meta metauniverse female safety problems occur frequently. How to solve the related problems in the metauniverse?
Experimental animal models - current market situation and future development trend
Who moved my code!
What are the advantages and disadvantages of data center agents?
JVM performance tuning and practical basic theory - medium
Query efficiency increased by 10 times! Three optimization schemes to help you solve the deep paging problem of MySQL
Pesticide synergist - current market situation and future development trend
Jerry's watch information type table [chapter]