Website foreground Verification Code (source code) implementation click Refresh

Source: Internet
Author: User

In order to facilitate the study of the site front-page readers have just opened their eyes on the decision to write their own verification code to write down for your reference on this basis can actually achieve the verification code of the operation, or the verification code of the picture display, other style verification code. Please make a lot of comments to provide your own ideas so that you can brainstorm to provide more ideas. OK, here's the code:

The code for the login.jsp page is mainly about how the intermediate Verification Code section is implemented:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Then there is the servlet that our verification code implements:

Package Userutil;import Javax.imageio.imageio;import javax.servlet.*;import javax.servlet.http.*;import java.io.*; Import java.awt.*;import java.awt.image.bufferedimage;import java.util.*;p ublic class Validatecodeservlet extends HttpServlet {/** * */private static final long Serialversionuid = 1l;/** in response to customer request */public void service (httpservletrequ EST request,httpservletresponse response) throws Servletexception, IOException {//Create memory Artboard object BufferedImage image= new  BufferedImage (100,30,BUFFEREDIMAGE.TYPE_INT_RGB);    Graphics G=image.getgraphics ();  Generate background color random r=new random ();    G.setcolor (New Color (R.nextint (255), R.nextint (255), R.nextint (255)));    Fill rectangle box with background color g.fillrect (0, 0, 100,30);    Call a custom method to get a string of alphanumeric combinations of length 5 number=getnumber (5);  The resulting random code is added to the session HttpSession Session=request.getsession ();    Session.setattribute ("code", number);  Set the color and font of the word, and graphically draw the G.setcolor (new Color (0,0,0));  G.setfont (New Font (null,font.bold,24));    g.DrawString (number,5,25); Draw the interference line foR (int i=0;i<8;i++) {G.setcolor (New Color (R.nextint (255), R.nextint (255), R.nextint (255)));  G.drawline (R.nextint), R.nextint (+), R.nextint (+), R.nextint (30));  }//Respond to user requests, draw graphics Response.setcontenttype ("Image/jpeg");  OutputStream Ops=response.getoutputstream ();  Imageio.write (image, "JPEG", OPS);      Ops.close (); } private String GetNumber (int size) {String str= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//Note that I only offer a A-Z in uppercase if you want to implement lowercase  Put yourself in there. The user's input should be converted to lowercase, whether uppercase or lowercase I did not have the interest to implement a very simple String number= "";  Random r=new random ();    for (int i=0;i<size;i++) {Number+=str.charat (R.nextint (Str.length ()));  } return number; }    }
Then configure your own web. XML to implement a simple verification code:

      <servlet>        <servlet-name>ValidateCodeServlet</servlet-name>        <servlet-class> userutil.validatecodeservlet</servlet-class>    </servlet>    <servlet-mapping>        < Servlet-name>validatecodeservlet</servlet-name>        <url-pattern>/validatecodeservlet</ Url-pattern>    </servlet-mapping>

The effect is as follows:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Website foreground Verification Code (source code) implementation click Refresh

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.