Random Verification Code implementation case

Source: Internet
Author: User

As we all know, the verification code in our lives are very common, many companies are in a variety of toss all kinds of verification code, here briefly with a small case to achieve the function of the Verification Code (PS: In fact, I really hate the verification code this thing).

Build a Javaweb project, the new login.html, in the main is written interface, code implementation is as follows, written in the body area on it, to a form validation

<body>    <form action= "" >    username:<input/><br/>    password:<input/>< br/>    Verification Code: <input name= "code"/>    <input type= "button" value= "can't see Clearly, change a" onclick= "Changes ()"/>    <br/>    <input type= "Submit" value= "Login"/>    </form>    <script type= "text/javascript" >    function Change () {    var Imageobj = document.getElementById ("Image1");    Address, the browser does not make a request    imageobj.src= "/day08_response/servlet/responsedemo4?time=" +new Date (). GetTime ();    </script>  </body>


Then we need to create a new servlet class, implement the verification code we can have two ways, one is to write their own, as follows: Color can be directly configured, well, given my aesthetic is limited, it seems that this color collocation is very ugly.

private void Test1 (HttpServletResponse response) throws IOException {int Width=110;int height=25;// 1 Build a picture of memory Bufferedimagebufferedimage bi=new bufferedimage (width, height, bufferedimage.type_int_bgr);//3. The paintbrush on the picture G=bi.getgraphics (); G.setcolor (Color.green); g.drawrect (0, 0, width, height);   Draw a rectangular edge//4 fill the background color g.setcolor (color.gray), G.fillrect (1, 1, width, height),//5 interference line G.setcolor (Color.yellow); Random r=new random (); for (int i=0;i<20;i++) G.drawline (r.nextint (width), r.nextint (height), r.nextint (width), R.nextint (height));//6 Digital Verification Code G.setcolor (Color.Black); G.setfont (New Font ("Arial", font.bold| font.italic,13));//Bold Italic int x=20;for (int i=0;i<4;i++) {g.drawstring (R.nextint) + "", x,20); x+=20;} 2 output stream Imageio.write (bi, "JPG", Response.getoutputstream ()) of the byte stream output of the response object;}

There is also a way to use, open source framework Validate.jar, import this jar, two lines of code can be done, of course, if you want to better effect, you can write your own or look inside the. Class source code, make your own changes, here is not much nonsense.

The open source framework implements Validatecode Vc=new Validatecode (110, 25, 4, 30);//The first parameter is wide, the second is high, the third is the number of verification codes, and the fourth is the number of lines in the interference line Vc.write ( Response.getoutputstream ());

Finally, we need to clear the browser cache, we all know, clear the cache is of course the three ways.

Clear Cache Response.setintheader ("Expires",-1), Response.setheader ("Cache-control", "No-cache"), Response.setheader (" Pragma "," No-cache ");

Oh? You are not satisfied, OK, then we add a regular refresh function!

The first type of refresh is a direct refresh:

private void Test1 (HttpServletResponse response) throws IOException {Response.setintheader ("Refresh", 1);//unit is seconds random R = new Random (); Response.getwriter (). Write (R.nextint () + "");
Of course, we can also come to that countdown a few seconds to jump to other pages of this effect,

Refresh elsewhere private void test2 (HttpServletResponse response) throws IOException {Response.setcontenttype ("text/html; Charset=utf-8 "); Response.getwriter (). Write (" Registered successfully! Automatically turn to landing page after 2 seconds. "); Response.setheader (" Refresh "," 2; Url=/day08_response/login.html ");//unit is seconds}
Now, let's take a look at how to control the browser cache time:

Response.setdateheader ("Expires", System.currenttimemillis () +1*60*60*1000);//The value is a millisecond value. If the value is less than the current time, it is not cached. If it is greater than the current time, the cache time is: value-the current time. Response.getwriter (). Write ("Hello");

Well, basically finished, the above knowledge will be able to realize the function of one of your projects, go and try it!

Random Verification Code implementation case

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.