JSP Verification Code

Source: Internet
Author: User

 

The implementation of the verification code is divided into two parts: JSP page and Java Servlet:

JSP page:

<! DOCTYPE html>

<Html>

<Head>

<Title> loginForm.html </title>

 

<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">

<Meta http-equiv = "description" content = "this is my page">

<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">

<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->

</Head>

 

<Body>

<H3> login page with verification code

<Form method = "post" action = "login. yan">

Username: <input type = "text" name = "usreName" size = "10"> <br/>

Password & nbsp; Code: <input type = "password" name = "password" size = "10"> <br>

Verification Code: <input type = "text" name = "userNum" size = "4"> <br/>

<Input type = "submit" value = "submit">

</Form>

</Body>

</Html>

 

Java class:

Package com. csdn. session;

Import java. awt. Color;

Import java. awt. Font;

Import java. awt. Graphics;

Import java. awt. image. BufferedImage;

Import java. io. ByteArrayOutputStream;

Import java. io. IOException;

Import javax. imageio. ImageIO;

Import javax. servlet. ServletException;

Import javax. servlet. ServletOutputStream;

Import javax. servlet. http. HttpServlet;

Import javax. servlet. http. HttpServletRequest;

Import javax. servlet. http. HttpServletResponse;

Import javax. servlet. http. HttpSession;

Public class loginForm extends HttpServlet {

Private static final long serialVersionUID = 1L;

Public void doGet (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

DoPost (request, response );

}

 

Private static int WIDTH = 80;

Private static int HEIGHT = 20;

Public void doPost (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

Response. setContentType ("image/jpeg ");

 

ServletOutputStream out = response. getOutputStream ();

 

// PrintWriter out = response. getWriter ();

Response. setHeader ("Pragma", "no-cache ");

Response. setHeader ("Cache-Controll", "no-cache ");

Response. setIntHeader ("Expires", 0 );

// Background

 

BufferedImage bi = new BufferedImage (WIDTH, HEIGHT, BufferedImage. TYPE_INT_RGB );

Graphics g = bi. getGraphics ();

DrawBackground (g );

 

// Generate a random Verification Code

Char [] rands = generateCheckCode ();

 

// System. out. println (fun (rands ));

 

HttpSession session = request. getSession ();

 

Session. setAttribute ("num", fun (rands) + ""); // verification code = 12-9 = 3; write the sesion object

 

DrawRands (g, rands );

 

ByteArrayOutputStream bos = new ByteArrayOutputStream ();

 

ImageIO. write (bi, "JPEG", bos );

 

Byte [] buf = bos. toByteArray (); // convert the content of the bi object into characters

 

Out. write (buf );

 

Response. setContentLength (buf. length );

 

Out. flush ();

Out. close ();

}

 

 

Private int fun (char [] rands ){

 

Int num1 = Integer. parseInt (rands [0] + "") * 10 + Integer. parseInt (rands [1] + "");

Int num2 = Integer. parseInt (rands [2] + "");

Int num = 0;

If (rands [2] = '+ '){

Num = num1 + num2;

} Else {

Num = num1-num2;

}

 

Return num;

 

}

 

 

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

 

G. setColor (Color. black );

G. setFont (new Font (null, Font. ITALIC | Font. BOLD, 18 ));

 

G. drawString ("" + rands [0], 1, 20 );

G. drawString ("" + rands [1], 14,17 );

G. drawString ("" + rands [3], 30,16 );

G. drawString ("" + rands [2], 42,18 );

G. drawString ("" + rands [4], 56,16 );

 

}

 

Private char [] generateCheckCode (){

 

String chars = "0123456789 ";

String char1 = "+ -";

Char [] rands = new char [5];

Rands [0] = chars. charAt (int) (Math. random () * 2) + 1 );

Rands [1] = chars. charAt (int) (Math. random () * 10 ));

Rands [2] = chars. charAt (int) (Math. random () * 9) + 1 );

Rands [3] = char1.charAt (int) (Math. random () * 2 ));

Rands [4] = ';

Return rands;

}

 

Private void drawBackground (Graphics g ){

G. setColor (new Color (0 XDCDCDC ));

 

G. fillRect (0, 0, WIDTH, HEIGHT );

 

For (int I = 0; I <20; I ++ ){

Int x = (int) (Math. random () * WIDTH );

Int y = (int) (Math. random () * HEIGHT );

Int red = (int) (Math. random () * 255 );

Int greed = (int) (Math. random () * 255 );

Int blue = (int) (Math. random () * 255 );

G. setColor (new Color (red, greed, blue ));

G. drawOval (x, y, 1, 0 );

}

 

}

}

From song lixing's column

Related Article

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.