JSP validation code generation and Refresh

Source: Internet
Author: User
Tags set background

About JSP verification code generation, display, validation and refresh: 1: Four-bit data validation picture generation, implemented in servlet

2: Configuring Servlet in Web.xmlli

3: Display a servlet-generated picture on the page

4: Verification of verification code input data

5: The verification code of the refresh

——————————————————————–

1: Four-bit data validation image generation, implemented in servlet, servlet code as follows:
Import java.io.*;
Import javax.servlet.http.*;
Import javax.servlet.*;
Import java.util.*;

Import java.awt.*;
Import java.awt.image.*;
Import javax.imageio.*;

public class Vimage extends HttpServlet {
public void init (ServletConfig conf) throws Servletexception {
Super.init (conf);
}

public void Doget (HttpServletRequest req, httpservletresponse Res) throws Servletexception, IOException {
Res.setcontenttype ("Image/jpeg");
Res.setheader ("Pragma", "No-cache");
Res.setheader ("Cache-control", "No-cache");
Res.setdateheader ("Expires", 0);
HttpSession session = Req.getsession ();

Create an image in memory
int width =, height = 20;
BufferedImage image = new BufferedImage (width, height, bufferedimage.type_int_rgb);

Get 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 lines of interference, so that the image of the authentication code is not easily detected by other programs
G.setcolor (Getrandcolor (160, 200));
for (int i = 0; I 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);
}
}

2: Configure servlet in Web.xmlli:

Vimage
/vimage

Note: Note the position of the element in the Web.xml file

3: Display a servlet-generated picture on the page:


Note: Add this to the place where you need to display the verification code, and name will be used when the verification code is refreshed.

4: Verification code input data validation:

Verification code generated in the session, is the servlet in this sentence Session.setattribute ("Post_validate_code", SRand), the specific verification is not much to say.
5: The verification code of the refresh
Refresh Verification Code

function reloadimage (URL)
{
Document.form name. pic.src = URL;
}

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.