Jsp+ajax implementation of local refresh verification code (onblur event triggering more than the test) _jsp programming

Source: Internet
Author: User
Tags rand set background

The example of this article is about the local refresh verification Code of JSP+AJAX implementation (onblur Event trigger). Share to everyone for your reference, specific as follows:

Foreground Display page:

welcome.jsp

<%@ page language= "java" contenttype= "text/html; Utf-8 "pageencoding=" Utf-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

Create a picture page

authcode.jsp

<%@ page contenttype= "image/jpeg" import= "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding = "GBK"%> <%!  
  Color Getrandcolor (int fc, int BC) {//The given range gets the random color Random Random = new Random ();  
  if (FC > 255) FC = 255;  
  if (BC > 255) BC = 255;  
  int r = FC + Random.nextint (BC-FC);  
  int g = FC + Random.nextint (BC-FC);  
  int B = FC + Random.nextint (BC-FC);  
 return new Color (R, G, b);  
 }%> <%//Settings page does not cache Response.setheader ("Pragma", "No-cache");  
 Response.setheader ("Cache-control", "No-cache");  
 Response.setdateheader ("Expires", 0);  
 Create image int width = 20 in memory;  
 BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);  
 Gets the graphics context Graphics g = image.getgraphics ();  
 Generate Random class Random Random = new Random ();  
 Set Background color G.setcolor (Getrandcolor (200, 250));  
 G.fillrect (0, 0, width, height); Set font g.setfont (new Font ("Times New Roman", Font.plain, 18));  
 Draw a border//g.setcolor (new Color ());  
 G.drawrect (0,0,width-1,height-1);  
 Randomly generated 155 interference lines, so that the authentication code in the image is not easy to be detected by other programs G.setcolor (Getrandcolor (160, 200));  
  for (int i = 0; i < i++) {int x = random.nextint (width);  
  int y = random.nextint (height);  
  int xl = Random.nextint (12);  
  int yl = Random.nextint (12);  
 G.drawline (x, y, X + xl, y + yl); 
 //Take the randomly generated authentication code (4 digits) String SRand = "";  
  for (int i = 0; i < 4; i++) {String rand = string.valueof (Random.nextint (10));  
  SRand + rand; Display the authentication code to the image G.setcolor (new Color (+ random.nextint), + random. Nextint (110))  
  //Call function out of the same color, may be because the seed is too close, so only directly generated g.drawstring (rand, * i + 6, 16); 
 System.out.println (RAND);  
 //The authentication code is stored in session session.setattribute ("code", SRand);  
 Image entry into force g.dispose ();  
Output image to page imageio.write (image, "JPEG", Response.getoutputstream ());

 %>

Ajax Dynamic Validation Page

codevalidate.jsp

<%@ page language= "java" import= "java.util.*,java.util.*,java.text.*,com.neusoft.xkxt.dao.*, Com.neusoft.xkxt.bean.*,com.neusoft.xkxt.util.* "pageencoding=" UTF-8 "%> 
<% 
String Path = Request.getcontextpath (); 
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; 
%> 
<% 
 String authcode=request.getparameter ("Authcode"); 
 String code= (String) session.getattribute ("code"); 
 if (authcode.equals (code)) { 
 out.println ("<div class= ' right ' >√</div>");} 
 else{ 
 out.println ("<div class= ' wrong ' > Authenticode incorrect </div>");} 
%>

I hope this article will help you with JSP program design.

Related Article

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.