In the design of the form, the increase of the verification code can be realized whether it is "artificial" operation, increase the verification code can prevent web database information jumbled, etc...
Now, the gadget will tell you how to implement the CAPTCHA through the servlet:
Verification code as a picture, in the page for "painting" out of, how it is drawn out?
< generate images >
{
The class that generated the picture:
1.BufferedImage image Data Buffer
2.Graphics Drawing Pictures
3.color Get Color
4.Random Get random numbers
5.ImageIO output Picture
}
///////////////////////////////////////////////////////////////////////////
< generate a captcha picture >
1. Write the following code in INDEX.JSP
<formAction= "<%= request.getcontextpath ()%>/servlet/loginservlet"Method= "Get" >Verification Code:<inputtype= "text"name= "Checkcode"/> <imgalt= "Verification Code"ID= "Imagecode"src= "<%= request.getcontextpath ()%>/servlet/imageservlet"/> <ahref= "Javascript:reloadcode ();">Can not see clearly</a><BR> <inputtype= "Submit"value= "Submit"> </form>
2. Create the Imageservlet class in SRC
1 Public classImageservletextendshttpservlet{2 Public voidDoget (httpservletrequest request,httpservletresponse response)throwsioexception{3 //This method implements the verification code generation4BufferedImage bi=NewBufferedImage (22,BUFFEREDIMAGE.TYPE_INT_RGB);//Creating an image buffer5Graphics G=bi.getgraphics ();//Create a canvas from a buffer6Color c=NewColor (200,150,255);//Create color7 /*draw a rectangle based on the background8 */9G.setcolor (c);//Create a background color for the canvasTenG.fillrect (0, 0, 68,22);//FillRect: Fills the specified rectangle One A Char[] ch= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789". ToCharArray ();//an array converted to a character type -Random r=NewRandom (); - intlen=ch.length; the intIndex//index is used to store random numbers -StringBuffer sb=NewStringBuffer (); - for(inti=0;i<4;i++) - { +Index=r.nextint (len);//Generate random numbers -G.setcolor (NewColor (R.nextint), R.nextint (188), R.nextint (255));//Set Color +g.DrawString (ch[index]+ "", (i*15) +3, 18);//draw numbers and positions of numbers A sb.append (Ch[index]); at } -Request.getsession (). SetAttribute ("Piccode", sb.tostring ());//keep numbers in session for later use -Imageio.write (BI, "JPG", Response.getoutputstream ()); -}
3. configuring Files in Web-inf
1 <servlet>2 <Servlet-name>Imageservlet</Servlet-name>3 <Servlet-class>Code.sam.ImageServlet</Servlet-class>4 </servlet>5 6 <servlet-mapping>7 <Servlet-name>Imageservlet</Servlet-name>8 <Url-pattern>/servlet/imageservlet</Url-pattern>9 </servlet-mapping>
------Ornate Split Line-------
< Enter information with the numbers in the picture to verify the >
1. Create a Loginservlet class
1 Public classLoginservletextendsHttpServlet {2 Public voidDoget (httpservletrequest request,httpservletresponse response)throwsIOException3 //used to verify the verification code4 {5String piccode= (String) request.getsession (). getattribute ("Piccode");6String checkcode=request.getparameter ("Checkcode");//Take value7 //checkcode=checkcode.touppercase (); //converts all characters to uppercase (this statement can be used to verify that the code is case-insensitive)8Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");//Solve garbled problems9PrintWriter out=Response.getwriter ();Ten if(Checkcode.equals (piccode)) One { AOut.println ("Verify code entered correctly! "); - } - Else the { -Out.println ("Verification code input Error!!! "); - } -Out.flush ();//Refresh the stream +Out.close ();//turn off the stream -}
2. Configure Web-inf
1 <servlet>2 <Servlet-name>Loginservlet</Servlet-name>3 <Servlet-class>Code.sam.LoginServlet</Servlet-class>4 </servlet>5 6 <servlet-mapping>7 <Servlet-name>Loginservlet</Servlet-name>8 <Url-pattern>/servlet/loginservlet</Url-pattern>9 </servlet-mapping>
------Ornate Split Line-----
When you click on the page to see the unclear, you need to refresh, write a section of the page JS code
1 function Reloadcode () 2 {3 var time=New Date (). GetTime (); 4 document.getElementById ("Imagecode"). src= "<%= Request.getcontextpath ()%>/servlet/imageservlet?d=" + Time; 5 }
****************************************************
The code is all done
Results such as * * *
JSP implementation verification code using servlet