Using Java to implement verification code function

Source: Internet
Author: User

Science and Technology excellent home 2017-06-12 12:10

Yesterday on the Internet saw an article on the verification code, very good, but some unsatisfactory place, such as not to consider the front end pass through the verification code if it is lowercase, then do the verification when the error occurs,

Because Java is strictly case-sensitive, there is the case that the verification code will overlap together is not considered, and the verification code can not see the refresh function did not do it

Here I make the following modifications:

First you need to import the Validatecode.jar package

1 package Com.wqy.ValidateCode; 2 3 Import java.io.IOException; 4 5 Import Javax.imageio.ImageIO; 6 Import javax.servlet.ServletException; 7 Import Javax.servlet.http.HttpServlet; 8 Import Javax.servlet.http.HttpServletRequest; 9 Import Javax.servlet.http.HttpServletResponse; Ten import Cn.dsna.util.images.ValidateCode; public class ValidateCode1 extends HttpServlet {in public void doget (HttpServletRequest request, HTTPSERVLETRESP Onse response) throws Servletexception, IOException {17//control do not cache the Response.setheader ("Expires", "1"); response.s Etheader ("Cache-control", "No-cache"); Response.setheader ("Pragma", "No-cache"); int width=120;//width int height=25;//height 25 int linecount=9;//number of interference lines (number of codecount=4;//)//Set Verification code + Validate Code VC = new Validatecode (width, height, codecount, linecount); //validatecode (120,25,4,9); String code = Vc.getcode; 29//The verification code is stored in the attribute domain ("code", code) Request.getSession.setAttribute; 31//Output Imageio.write (VC. getbuffimg, "JPG", response.getoutputstream); DoPost (HttpServletRequest request, httpservletresponse response) Notoginseng throws Servletexception, IOE xception {doget (request,response); 39} 40 41}

Here is the HTML code:

<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%> <% String path = Request.getcontextpath; String BasePath = request.getscheme+ "://" +request.getservername+ ":" +request.getserverport+path+ "/"; %> 

Servlet code for background validation:

1 package Com.wqy.ValidateCode; 2 3 Import java.io.IOException; 4 5 Import Javax.servlet.ServletException; 6 Import Javax.servlet.http.HttpServlet; 7 Import Javax.servlet.http.HttpServletRequest; 8 Import Javax.servlet.http.HttpServletResponse; 9 public class Loginservlet extends HttpServlet {one public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {13//Get the verification code from the front end String name = Request.getparameter ("Captcha"); 15/ /Obtain the stored verification code from the attribute domain, String code = (string) request.getSession.getAttribute ("code"); 17//Front-end verification code for comparison. (Verification code. toLowerCase is to let it ignore the case, so that when the front end does not cause an exception to be thrown because of a casing problem), if (Code.toLowerCase.equals (name.tolowercase)) {19 System.out.println ("Verification code is correct"); The public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, I oexception {doget (request, response); 26} 27 28}

Final Display Result:

Using Java to implement verification code function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.