Session instance: one-time verification code

Source: Internet
Author: User

 

The cookie function is not disabled.

URL rewriting

When a request is submitted for the first time, when the request message does not contain the cookie of the session, the URL rewriting technology is used to generate a response.

When you submit a request later, the request message will contain the session cookie.

The cookie function is not disabled: All response messages generated are rewritten using URLs.

 

Session instance: one-time verification code

Loginform.html

Checkcodeservlet: generate a verification code in the Session Object

Loginformservlet: Check whether the entered verification code is consistent with the verification code displayed in the image.

Implementation Code:

Checkcodeservlet

Private Static final long serialversionuid = 1l;

Private Static int width = 60;

Private Static int Height = 20; // The image height is 20 pixels.

Doget Method

Response. setcontenttype ("image/JPEG"); // type of the response content

 

Servletoutputstream out = response. getoutputstream ();

 

Httpsession session = request. getsession (); // obtain the session object

 

// The Verification Code image cannot be cached on the client

Response. setheader ("Pragma", "No-Cache ");

Response. setheader ("cache-controll", "No-Cache ");

Response. setintheader ("expires", 0); // set the expiration time

 

// Drawing -- opening a buffer in the memory ---- background

Bufferedimage Bi = new bufferedimage (width, height,

Bufferedimage. type_int_rgb); // specifies the width and height of the image.

Graphics G = Bi. getgraphics (); // obtain the paint brush by calling the method of drawing paper.

 

Drawbackground (g );

 

// Generate a random Verification Code

Char [] rands = generatecheckcode (); // a four-digit verification code is randomly generated.

 

// Output, painted on paper

Drawrands (G, rands );

 

Bytearrayoutputstream Bos = new bytearrayoutputstream ();

ImageIO. Write (Bi, "Jpeg", Bos); // format of jepg Conversion

// Define a byte array

Byte [] Buf = Bos. tobytearray (); // converts the content in the Bi object to a byte array

Out. Write (BUF );

 

// Set the length of the response content

Response. setcontentlength (BUF. Length );

 

Session. setattribute ("checkcode", new string (rands ));

 

Out. Flush ();

Out. Close ();

}

Drawrands Method

Private void drawrands (Graphics g, char [] rands ){

// Paint brush color

G. setcolor (color. Black );

// Font of the output character, which is bold and skewed. The font size is 18.

G. setfont (new font (null, Font. italic | font. Bold, 18 ));

// Output characters

G. drawstring ("" + rands [0], 1, 15); // contains 1st characters

G. drawstring ("" + rands [1], 16, 14); // contains 2nd characters

G. drawstring ("" + rands [2], 31, 18); // contains 3rd characters

G. drawstring ("" + rands [3], 46, 12); // contains 4th characters

System. Out. println ("================ ");

System. Out. println (new string (rands ));

}

Generatecheckcode Method

Private char [] generatecheckcode (){

String chars = "0123456789 abcdefghijklmnopqrstuvwxyz"; // put all possible characters in an array

// Define an array for storing random numbers

Char rands [] = new char [4];

For (INT I = 0; I <4; I ++ ){

Int random = (INT) (math. Random () * 36); // randomly generates subscript in Chars

Rands [I] = chars. charat (random );

}

Return rands;

}

Drawbackground Method

Private void drawbackground (Graphics g ){

// Draw the background

G. setcolor (new color (0 xdcdcdc ));

G. fillrect (0, 0, width, height); // filled rectangle, top left corner and bottom right corner vertex coordinates

 

// Draw interference lines

For (INT I = 0; I <120; I ++ ){

// Randomly generate data, coordinates in the upper left corner

Int x = (INT) (math. Random () * width );

Int y = (INT) (math. Random () * Height );

 

// The elliptical color is random.

Int Red = (INT) (math. Random () * 255 );

Int Green = (INT) (math. Random () * 255 );

Int Blue = (INT) (math. Random () * 255 );

G. setcolor (new color (red, green, blue ));

 

G. drawoval (X, Y, 1, 0); // randomly generates elliptical lines, not filled

}

}

Loginformservlet

Httpsession session = request. getsession ();

String code = request. getparameter ("checkcode ");

String value = session. getattribute ("checkcode"). tostring ();

If(Value. Equals (CODE )){

Out. println ("correct Verification Code ");

}Else{

Out. println ("<font color = Red> Incorrect verification code </font> ");

}

Loginform.html

<H3> logon page with verification code

<Form method ="Post"Action ="Loginformservlet"Name ="Form1">

Username: <input type ="Text"Name ="Username"/></BR>

Password & nbsp; Code: <input type ="Password"Name ="Password"/></BR>

Verification Code: <input type ="Text"Name ="Checkcode"/>

<IMG src ="Servlet/checkcode"/></BR>

<Input type ="Submit"Value ="Submit"/>

</Form>

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.