I believe everyone is familiar with verification codes. Whether you apply for an account or log in in some cases, you will be required to enter the verification code. After statistics, the probability of successfully passing the verification code in one verification is 90%, which is not high. So many people will definitely doubt the necessity of this design that reduces user experience, but Hegel said: Everything that is reasonable is realistic; everything that is realistic is rational. Next, let’s learn about the verification code.
Verification code is a public fully automatic program that distinguishes whether a user is a computer or a person. It is used to prevent malicious password cracking, ticket swiping, and forum flooding, and prevent hackers from constantly logging in through brute force cracking. It is used in banks, communities, forums, voting systems, etc.
Without further ado, let’s take a look at the four ways I know to implement verification code in Java.
Method 1:
The first method was the first thing I thought of, and it was the simplest implementation logic, but it was extremely efficient and safe.
The specific operation is: 1. Use photoshop to create a verification code picture. The rectangular picture may have necessary English letters, numbers or Chinese (as above)
2. Display the image in the swing control or jsp page
3. Match the corresponding verification code string for each image in the code
4. When submitting, get the string in the component or text box and the string of each picture to compare it with the equals() method.
The disadvantage is that the process of making verification code pictures is too time-consuming, and the implementation method is extremely low, and it is highly recommended to implement it in this way. The following method will become more efficient, beautiful and relatively safe.
Method 2:
Here we talk about Java Web and the verification code implementation under Servlet, and the logic is still very clear.
To eliminate the simpler implementation code, let’s start with the front-end key code:
When we click "can't see clearly", the verification code image will be refreshed, and a js function will be called to reset the image path to replace the image. Please see the code below. In the code, <%=request.getContextPath()%> is to solve the problem of relative paths and can return the root path of the site. /servlet/ImageServlet is a whole, pointing to the servlet of ImageServlet. Why do we need to add a /servlet before? Because we have made configuration mapping in web.xml, it can be understood as changing a longer name. Then look at the following js function. Some people may have questions, why get a current time and add it to the end of the path? In fact, this is to solve the problem of browser cache. It is the problem that the verification code image has been changed after the ImageServlet is triggered, but the cache has not changed. With the help of different times every moment, the browser cache can be invalidated.
<script type="text/javascript"> function reloadCode(){ var time = new Date().getTime(); document.getElementById("imagecode").src="<%=request.getContextPath() %>/servlet/ImageServlet?d="+time; } </script>Here are the key configuration information in web.xml:
<script type="text/javascript"> function reloadCode(){ var time = new Date().getTime(); document.getElementById("imagecode").src="<%=request.getContextPath() %>/servlet/ImageServlet?d="+time; } </script>Then let's look at how the key ImageServlet generates images:
<servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>com.muke.ImageServlet</servlet-class> </servlet> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.muke.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/ImageServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/LoginServlet</url-pattern> </servlet-mapping>
If you want to describe more vividly how this verification code is implemented, then just one word "draw" sounds similar to the first method, and it is still relatively low, but the efficiency of using code to automatically "draw" the verification code has definitely increased countless times. Let's look at the above code. First, a BufferedImage object bi is instantiated. Bi is used to draw the verification code picture. Then, using bi to get a brush g, and use g to draw the rectangular background of the entity. Then, using simple logic to call the commonly used drawString() method in Java to draw the verification code characters on the rectangle. At the same time, the string is added to the StringBuffer variable string object in sequence, and finally stored in the jsp built-in object session for the comparison after submitting the verification code. In order to display the verification code, we also need to write the generated verification code image to the ImageIO stream in a certain image format.
As can be seen from LoginServlet below, you can compare the string in the verification code submission box by obtaining the string that has just been stored in the ImageServlet. You can change the strings to lowercase or uppercase to ignore uppercase.
public class ImageServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ BufferedImage bi = new BufferedImage(,,BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); Color c = new Color(,,); g.setColor(c); g.fillRect(, , ); char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); Random r = new Random(); int len=ch.length,index; StringBuffer sb = new StringBuffer(); for(int i=; i<; i++){ index = r.nextInt(len); g.setColor(new Color(r.nextInt(),r.nextInt(),r.nextInt(),r.nextInt())); g.drawString(ch[index]+"", (i*)+, ); sb.append(ch[index]); } request.getSession().setAttribute("piccode", sb.toString()); ImageIO.write(bi, "JPG", response.getOutputStream()); } }Here is the implementation legend:
The above is the relevant knowledge about Java verification code production introduced to you by the editor. I hope it will be helpful to you! I will introduce to you the Java verification code production (Part 2). Interested friends, please pay attention to the Wulin.com website!