Java Verification Code Detailed

Source: Internet
Author: User

1 using the servlet to implement the verification code, the knowledge points involved in Java drawing technology and session to save data.

HTML page

1 <HTML>2 <Imagesrc= ' images/logo1.jpg '/><HR/>3 <Head><BR/><title>Login</title> <BR/><H1>Welcome to Login</H1></Head>4 <Body>5 <formAction= '/loginvalid/loginverify 'Method= ' Post '>6User id:<inputtype= ' text 'name= ' UserID 'value= ' '>7User password:<inputtype= ' Password 'name= ' Password 'value= ' '>9Verification Code:<inputtype= ' text 'name= ' Inputcode '/>  <imgID= ' Imgobj 'src= '/loginvalid/createcode2 '/><ahref= ' javascript:changeimg () '>Another one.</a>Ten <inputtype= ' Submit 'value= ' Login '/> One </form> A </Body>
- <Scripttype= ' Text/javascript '> - functionchangeimg () {
var Time= NewDate ();
document.getElementById ('imgobj'). SRC='/loginvalid/createcode2?d='+Time ;
} the </Script> - </HTML>

  

Tool class for generating verification code

1  Public classCodeutil {2     3     //Picture Width4     Private Static Final intimg_w=120;5     //Picture Height6     Private Static Final intImg_h=30;7     //number of verification code characters8     Private Static Final intNum_chs=5;9     //Verification code contains the specific charactersTen     Private Static Char[] CHS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890". ToCharArray (); One     Private StaticRandom Rand =NewRandom (); A      -     //image X-axis, y-axis position cardinality -     Private Static intCodeX = 20; the     Private Static intCodey = 25; -      -     //Font Size -     Private Static intFonth = 25; +      -      Public StaticMap<string, object>generateimage () +     { ABufferedImage image =Newbufferedimage (img_w, Img_h, Bufferedimage.type_int_rgb); atGraphics g =image.getgraphics (); -          -         //Set picture background color to white - G.setcolor (color.white); -G.fillrect (0, 0, img_w, img_h); -          in         //Set Font size -Font font =NewFont ("Fixedsys", Font.Bold, fonth); to G.setfont (font); +          -         //Draw Border the G.setcolor (color.black); *G.drawrect (0, 0, img_w, img_h); $         Panax Notoginseng G.setcolor (color.black); -         //randomly generates 30 lines of interference the          for(inti=0; i<30; i++) +         { A             intx =Rand.nextint (img_w); the             inty =Rand.nextint (img_h); +             intXL = Rand.nextint (20); -             intYL = Rand.nextint (20); $              $ g.drawline (x, Y, XL, yl);  -         } -          theStringBuilder SB =NewStringBuilder (); -          for(inti=0; i<num_chs; i++)Wuyi         { the             //randomly get a character from a string -String Code =string.valueof (Chs[rand.nextint (chs.length)); Wu             //randomly set brush color -G.setcolor (NewColor (Rand.nextint (255), Rand.nextint (255), Rand.nextint (255))); Aboutg.DrawString (Code, (i+1) *CodeX, Codey); $ sb.append (code); -         } -          -map<string, object> map =NewHashmap<string, object>(); AMap.put ("Code", sb.tostring ()); +Map.put ("Codeimage", image); the         returnmap; -          $     } the  the}

Servlet that generates a verification code in the background

1         //prevent browser from caching random pictures2Response.setdateheader ("Expires", 1);3Response.setheader ("Cache-control", "No-cache");4Response.setheader ("Pragma", "No-cache");5         6         //notifies the client to open a picture to send past data7Response.setheader ("Content-type", "Image/jpeg");8         9map<string, object> map =codeutil.generateimage ();Ten         //Save the verification code to session OneRequest.getsession (). SetAttribute ("Checkcode", Map.get ("code")); AImageio.write ((renderedimage) map.get ("Codeimage"), "JPG", Response.getoutputstream ());

Background verifies that the user entered a validation code that is consistent with the displayed servlet class

2HttpSession session =request.getsession ();3String secode = (string) session.getattribute ("Checkcode");4String inputcode = (string) request.getparameter ("Inputcode");5         if(Secode.equals (inputcode))6         {7Request.getrequestdispatcher ("/main"). Forward (request, response);8         }9         ElseTen         { OneRequest.getrequestdispatcher ("/err"). Forward (request, response); A}

  

Java Verification Code Detailed

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.