This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13237|Reply: 7

[JavaEE] How to implement verification code verification on the landing page?????

[Copy link]
Posted on 3/19/2019 12:16:43 AM | | |
<html页面>. .
<tr>
      <tdclass="td_left">
         <labelfor="check">CAPTCHA</label>
      </td>
      <tdclass="td_right check">
         <inputtype="text"id="check"name="check"class="check">
         <imgsrc="checkCodeServlet"height="32px"alt=""onclick="changeCheckCode(this)">
<scripttype="text/javascript">
   //Image click events
   functionchangeCheckCode(img) {
      img.src="checkCodeServlet?"+newDate().getTime();
                               }
</script>
      </td>
   </tr>//验证码拼接部分public classCheckCodeServletextendsHttpServlet{
   @Override
   public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException{
      
      //The server informs the browser not to cache
      response.setHeader("pragma","no-cache");
      response.setHeader
("cache-control","no-cache");
      response.setHeader
("expires","0");
      
      
//Create a long one in memory80, wide30The default black background
      //Parameter 1: Length
      //Parameter 2: Width
      //Parameter 3: Color
      intwidth =80;
      
intheight =30;
      BufferedImage image =
newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
      
      
//Get the brush
      Graphics g = image.getGraphics();
      
//Set the brush color to gray
      g.setColor(Color.GRAY);
      
//Fill the picture
      g.fillRect(0,0, width,height);
      
      
//Produced4random verification code,12Ey
      String checkCode = getCheckCode();
      
//Put the code inHttpSessionMedium
      request.getSession().setAttribute("code",checkCode);
      
      
//Set the brush color to yellow
      g.setColor(Color.YELLOW);
      
//Set the size of the font
      g.setFont(newFont("Black body",Font.BOLD,24));
      
//Write a verification code to the image
      g.drawString(checkCode,15,25);
      
      
//Output the picture from memory to the browser
      //Parameter 1: Picture object
      //Parameter 2: The format of the picture, such as:PNG,JPG,GIF
      //Parameter 3: Where the picture is output
      ImageIO.write(image,"PNG",response.getOutputStream());
   
}
   /**
    *Produced4Bit random string
    */
   privateString getCheckCode() {
      String base ="0123456789ABCDEFGabcdefg";
      
intsize = base.length();
      Random r =
newRandom();
      StringBuffer sb =
newStringBuffer();
      
for(inti=1; i<=4; i++){
         //Produced0tosize-1of random values
         intindex = r.nextInt(size);
         
//atbasestring get subscript asindexcharacters
         charc = base.charAt(index);
         
//willcPut inStringBufferGo in the middle
         sb.append(c);
      
}
      returnsb.toString();
   
}
   @Override
   public voiddoPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException{
      this.doGet(request,response);
   
}
}


How to achieve it?










Previous:Solution to catch packets after Android 7.0 unknown and invalid certificates
Next:"Writing High-Quality Code: 150 Suggestions for Improving C++ Programs" by Li Jian (author...
Posted on 3/19/2019 9:13:39 AM |


The principle is that when the user obtains the verification code, the verification code string is stored in the session, and when the user submits the verification code, it is compared with the string in the session.
Posted on 3/19/2019 9:41:21 AM |
When comparing, convert case
 Landlord| Posted on 3/20/2019 1:14:55 PM |
QWERTYU Posted on 2019-3-19 09:41
When comparing, convert case

Thank you, big brother
 Landlord| Posted on 3/20/2019 1:15:34 PM |
Xiao Zhazha Posted on 2019-3-19 09:13
Principle, when the user obtains the verification code, the verification code string is stored in the session, and when the user submits the verification code, the characters in the session ...

Thanks to the big guy Xiaobai's question is relatively low
Posted on 3/20/2019 1:24:54 PM |
Pu Zhibin Posted on 2019-3-20 13:15
Thanks to the big guy Xiaobai's question is relatively low

It's okay, they all came from Xiaobai
 Landlord| Posted on 3/20/2019 3:39:42 PM |
Xiao Zhazha Posted on 2019-3-19 09:13
Principle, when the user obtains the verification code, the verification code string is stored in the session, and when the user submits the verification code, the characters in the session ...

Brother, we have an assignment behind us, which is to do the three-level linkage of provinces and municipalities, how to do this, I will add you to WeChat
Posted on 3/20/2019 3:45:50 PM |
Pu Zhibin Posted on 2019-3-20 15:39
Brother, we have an assignment behind us to do the three-level linkage of provinces and cities, how should this be done, I will add you WeChat...

Do it yourself.

The HTML page has 3 select drop-down boxes, when the user selects the drop-down box, triggers the change event, requests data from the interface with the parameter ID, and dynamically adds the returned data to the next level of the drop-down box, the general idea is this.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com