JSP dynamically generated verification code

Source: Internet
Author: User

(1) In the login application, in order to prevent malicious login, it is often necessary for the server to dynamically generate the verification code and stored in the session scope, and finally returned to the client as an image display

(2) The following code implements the function: Write a JSP page, dynamically generate a verification code, stored in the session scope, and returned to the client as an image display.

Also write a JSP page, referencing the validation code generated by this JSP page;


The authen.jsp code is as follows:

<%@ page import= "java.awt.*,java.awt.image.*,java.util.*,com.sun.image.codec.jpeg.*"%><%!// Depending on the provided AB produces a random range of color variations of colour getColor (int a,int b) {int n=b-a;        Random rd=new random (), int cr=a+rd.nextint (n), int cg=a+rd.nextint (n), int cb=a+rd.nextint (n);        return new Color (CR,CG,CB); }%><%//Bottom three line cancels the function of the client browser cache captcha Response.setheader ("Pragma", "No-cache"); Response.setheader ("Cache-control", " No-cache "); Response.setdateheader (" Expires ", 0); int width=60, height=20;//generate an image in memory bufferedimage image = new BufferedImage (width, height, bufferedimage.type_int_rgb); Graphics g = image.getgraphics (); Random random = new Random (), G.setcolor (GetColor (200,250)), G.fillrect (0, 0, width, height), G.setfont (New Font ("Times New Roman ", font.bold,18)); G.setcolor (GetColor (160,200)); for (int i=0;i<160;i++) {int x = random.nextint (width); int        y = random.nextint (height);        int xl = Random.nextint (12); int yl = Random.nextint (n); G.drawline (x,y,x+xl,y+yl);} String Number=string.valueoF (1000+random.nextint (8999)); String name=request.getparameter ("name"); Session.setattribute (Name,number); G.setcolor (GetColor (20,130)); int x= ( int) (width*0.2); int y= (int) (height*0.8); g.drawstring (number,x,y); G.dispose ();          JPEGImageEncoder Encoder=jpegcodec.createjpegencoder (Response.getoutputstream ());       Encoder.encode (image);     Out.close (); %>


Build another test.jsp page call verification code:

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

(3) in the above two pages have the ability to cancel the client cache, this is because of the other browser, such as the use of the Internet Explorer Tour way,

The image will be placed in the cache first, when the request will now be in memory to find whether there is already, some words are not the request, which makes in the refresh test

When the code fails, so to make the viewer does not read the cached picture, you need to cancel the cache;

(4) ok! This is the end!



JSP dynamically generated verification code

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.