Jsp+servlet Implementing verification Code generation

Source: Internet
Author: User

The main use of jsp+servlet to achieve verification code generation

Asynchronous replacement of image address with jquery Ajax technology

BufferedImage implementation of verification code image generation

Servlet Code:

Package Register;import Java.awt.color;import java.awt.font;import java.awt.graphics;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.ioexception;import Java.util.Random;import Javax.imageio.imageio;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import javax.servlet.http.HttpSession;/** * Servlet implementation class GetCode*/ Public classGetCode extends HttpServlet {Private StaticFinalLongSerialversionuid =1L; /** * @see httpservlet#httpservlet ()*/     PublicGetCode () {super (); //TODO auto-generated Constructor stub    }    /** * @see httpservlet#doget (httpservletrequest request, httpservletresponse response)*/    protected voiddoget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method StubBufferedImage img =NewBufferedImage ( -, -, Bufferedimage.type_int_argb_pre); Graphics g=Img.getgraphics ();                              G.setcolor (Color.gray); G.fillrect (0,0, the, -);        Draw Verification Code Background g.setcolor (Color.White); G.setfont (NewFont ("Song Body", Font.Bold, -)); Random Rand=NewRandom (); String Code="";  for(intI=0;i<4; i++) {//randomly generate 4-bit digital CAPTCHA code+ = Rand.nextint (Ten); } g.drawstring (Code,5, -); Imageio.write (IMG,"PNG",NewFile ("d:/"+code+". PNG"));                 Generate a Captcha picture Response.getwriter (). println (code); Verification code back to foreground}/** * @see httpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method Stubdoget (Request,response); }}

Front JSP page:

<%@ page language="Java"Contenttype="text/html; Charset=utf-8"pageencoding="Utf-8"%><! DOCTYPE HTML Public"-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD">"Content-type"Content="text/html; Charset=utf-8"><title>insert title Here</title><script type="Text/javascript"Src="Js/jquery-1.10.2.js"></script><script type="Text/javascript">$ (document). Ready (function () {varCode; $.ajax ({//Receive CAPTCHA URL:"GetCode", type:"POST", Success:function (data, textstatus) {code=$.trim (data); $("img"). attr ({src:"d:/"+code+". PNG"});            Change Captcha Address}, Error:function (XMLHttpRequest, Textstatus, Errorthrown) {}}); $("#img"). Click (function () {//img tag to bind a point click event, click Change Picture $.ajax ({url:"GetCode", type:"POST", Success:function (data, textstatus) {code=$.trim (data); $("img"). attr ({src:"d:/"+code+". PNG"});    }, Error:function (XMLHttpRequest, Textstatus, Errorthrown) {}});    }); $("#code"). blur (function () {//To determine if the input verification code and the generated consistencyif($("#code"). val ()! =code) {            $("#code"). CSS ("Background-color","#F00"); }        Else{            $("#code"). CSS ("Background-color","#0f0");    }                }); });</script>"#">Account&nbsp;&nbsp;&nbsp;&nbsp; No. <input type="text"Name="username"Id="username"/><br><br>Dense&nbsp;&nbsp;&nbsp;&nbsp; Code <input type="Password"Name="Password"Id="Password"/><br><br>Verification Code<input type="text"Name="Code"Id="Code"/>"img"Src="#"><br><br> <input type="Submit"Value="Register"> </form></body>

Jsp+servlet Implementing verification Code generation

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.