Javaweb Creating a verification code using an HTML page

Source: Internet
Author: User

Verification code is required for login in the actual project, followed by the captcha technique used in my Web projectindex.html
1 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">2 <HTML>3   <Head>4     <title>Index.html</title>5     <Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3">6     <Metahttp-equiv= "description"content= "This is my page">7     <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">8     <!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" > -9   </Head>Ten    <Scripttype= "Text/javascript"> One    functionClk_image () { A     varimg=document.getElementById ("scodeimg");  - img.src= "validate.jsp?" +math.random (); - } the    </Script> -   <Body> -    <inputtype= "text"name= "Code"ID= "Code"/> -     <imgID= "Scodeimg"alt= "Please enter verification code"src= "validate.jsp?,math.random ();"onclick= "return Clk_image ();"width= " +"Height= "+"> +   </Body> - </HTML>
View Codevalidate.jsp
1<%@ page contenttype= "text ml;charset=utf-8" language= "java"%>2<%@ pageImport= "Java.awt.*"%>3<%@ pageImport= "Java.awt.image.*"%>4<%@ pageImport= "Java.util.*"%>5<%@ pageImport= "Javax.imageio.*"%>6  7<%! Color Getrandcolor (intFcintBC) {8Random random =NewRandom ();9         if(FC > 255)TenFC = 255; One         if(BC > 255) ABC = 255; -         intr = FC + random.nextint (BC-FC); -         intg = FC + random.nextint (BC-FC); the         intB = fc + random.nextint (BC-FC); -         return NewColor (R, G, b); -}%> -<% +Response.setheader ("Pragma", "No-cache"); -Response.setheader ("Cache-control", "No-cache"); +Response.setdateheader ("Expires", 0); A     intwidth = 60; at     intHeight = 20; -BufferedImage image =Newbufferedimage (width, height, - Bufferedimage.type_int_rgb); -Graphics g =image.getgraphics (); -Random random =NewRandom (); -G.setcolor (Getrandcolor (200, 250)); inG.fillrect (0, 0, width, height); -G.setfont (NewFont ("Times New Roman", Font.plain, 18)); toG.setcolor (Getrandcolor (160, 200)); +      for(inti = 0; I < 155; i++) { -         intx =random.nextint (width); the         inty =random.nextint (height); *         intX1 = Random.nextint (12); $         intY1 = Random.nextint (12);Panax NotoginsengG.drawline (x, y, x + x1, y +y1); -     } theString SRand = ""; +      for(inti = 0; I < 4; i++) { AString rand = string.valueof (Random.nextint (10)); theSRand + =Rand; +G.setcolor (NewColor (+ random.nextint (110), 20 +Random -. Nextint (+), + random.nextint (110))); $g.DrawString (Rand, * i + 6, 16); $     } -     //Save the Verification code to the session, and then check it out and compare the user input when the login is validated. -Session.setattribute ("Checkcode", SRand); the g.dispose (); -Imageio.write (Image, "JPEG", Response.getoutputstream ());Wuyi out.clear (); theout =pagecontext.pushbody (); -%>
View Code

Javaweb Creating a verification code using an HTML page

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.