As we all know, now login to register a variety of website accounts are required to enter a verification code. Setting a verification code, without a doubt, reduces the user experience, but why do various websites still use verification codes?
Obviously, the verification code has its special function: The verification code is a kind of public automatic program which distinguishes the user from the computer or human. It can prevent malicious password, brush tickets, forum irrigation, brush pages and so on. Think of, if there is no verification code, each login site only need user name and password, at this time, some sinister designs hackers can use brute force method to crack password, and hackers can constantly refresh access to the same site, so that its burden, serious when can cause the website crashes. Therefore, it is necessary to set the verification code.
Needless to say, I will introduce four kinds of common verification code implementation, respectively, using JSP and servlet to implement a simple alphanumeric verification code, the use of Kaptcha components to implement verification code, Chinese Verification Code implementation, arithmetic verification code implementation.
The implementation of verification code can be divided into several steps: Generate a verification code and render it, to determine whether the user input verification code is accurate, processing results.
(i) JSP and servlet implement simple alphanumeric verification code
First look at the final effect:
The final presentation is a random combination of numbers and letters.
The Eclipse project directory is as follows:
Here's a look at the detailed implementation:
The ① first implements the front-end rendering. Because the focus is on the background logic implementation, the front-end is relatively simple to write, with only one form:
The class that processes the form is Loginservlet, in Loginservlet, the user input is judged accurate, the CAPTCHA image is generated and rendered by Imageservlet, and the "unclear" function is realized by JavaScript, and the request is re-sent after clicking. It is important to note that, because of the role of caching, it is not replaced if it is only used to generate a single imageservlet. To implement the replacement function, you can add a suffix, which makes each request URL different (with random number sometimes error, for specific reasons please google).
The ②imageservlet class inherits the HttpServlet class, overriding the Doget method. To generate a verification code and render it as a picture, there are 6 main steps:
• Defining BufferedImage objects
• Get Graphics
• Generate random verification code information by random
• Draw pictures with graphics
• Record verification code information into session
• Output pictures using ImageIO
The code is as follows:
③loginservlet is relatively simple, only need to get the generated verification code and user input comparison through the session, and then each processing can be.
After writing the code, configure Web. XML to finish!!!
(ii) The use of Kaptcha components to achieve
It is easier to use the Kaptcha component because it will help you generate a CAPTCHA image. The first thing to import is the Kaptcha package.
The directory is as follows:
Index.jsp or similar, now img SRC can write fixed jpg,kaptcha will automatically help you to generate a different picture:
Kaptchaservlet.java is provided on the jar package and can be copied in the past.
CHECK.JSP Check the verification code is correct, here is not much to say, see below:
Finally, configure the Web. xml file. Kaptcha has a lot of properties can be configured: Captcha image size, text content and so many many, interested in in-depth study, here I only give the simplest example:
The results of the final operation are as follows:
Full code See: Https://github.com/lingd3/Captcha
Java Implementation Verification Code (top)