Implementation of image verification in JSP

Source: Internet
Author: User

[Preface]:Image verification is often used for registration and posting. I have written this article in jsp for your reference.Com. sun. imageIt is not a java standard package. You need to download it separately. The address is:

Http://java.sun.com/products/java-media/jai/

  1. Random. jsp (generates four random characters, consisting of 0-9, a-z, A-Z. and stores the final string in the session for subsequent page verification)

The Code is as follows:

<% @ Page autoFlush = "false" import = "java. util. *, java. awt. *, java. awt. image .*,
Com.sun.image.codec.jpeg. *, java. util. * "%>

<% @ Page import = "" contentType = "text/html; charset = gb2312" %>

<%

String chose =
"0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
Char display [] = {0, 0, 0, 0}, ran [] = {0, 0, 0}, temp;
Random rand = new Random ();
For (int I = 0; I <4; I ++)

{
Temp = chose. charAt (rand. nextInt (chose. length ()));
Display [I * 2] = temp;
Ran [I] = temp;

}



String random = String. valueOf (display );
Session. setAttribute ("random", String. valueOf (ran ));

%>

<%
Out. clear ();

Response. setContentType ("image/jpeg ");

Response. addHeader ("pragma", "NO-cache ");

Response. addHeader ("Cache-Control", "no-cache ");

Response. addDateHeader ("Expries", 0 );

Int width = 47, height = 15;

BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );

Graphics g = image. getGraphics ();

// Fill in the background color below

G. setColor (Color. GREEN );

G. fillRect (0, 0, width, height );

// Set the font color

G. setColor (Color. RED );

G. drawString (random, 3,10 );

G. dispose ();

ServletOutputStream outStream = response. getOutputStream ();

Required imageencoder encoder = required codec. createJPEGEncoder (outStream );

Encoder. encode (image );

OutStream. close ();

%>


There are two pages in this news. Currently, there are two pages in page 1st.


2. Img. jsp (the verification image is displayed. The verification program is also put together due to the simplicity of this Program)

The Code is as follows:

<% @ Page contentType = "text/html; charset = gb2312" language = "java" %>

<%

String num = request. getParameter ("num ");

String random = (String) session. getAttribute ("random ");
If (num! = Null & random! = Null)

{

If (! Num. equals (random ))

{

Out. println ("<script> alert (Incorrect verification code! Try again .) </Script> ");
Out. println ("<script> history. go (-1) </script> ");
// Response. sendRedirect ("img. jsp ");

}

Else

{

Out. println ("<center> Verification Successful! </Center> ");

}

}

%>

<Html>

<Head>

<Title> image verification </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

</Head>
<Body>

<Form action = "img. jsp" method = "post">

<Table>

<Tr>

<Td>

<Input type = "text" name = "num" size = 10>

</Td>

<Td>



</Td>

</Tr>

</Table>

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

</Form>

</Body>

</Html>


Description :Interested users can add image content on this basis. for example, add a texture to the background. skew character positions to increase the difficulty of scanning programs. any comments and suggestions can be put forward in the reply. ....

There are two pages in this news. Currently, there are two pages in page 2nd.


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.