Implementation of image verification in JSP

Source: Internet
Author: User
Tags rand string
Js

Photo Validation is often used in relation to registration and post messages. I have written a JSP here. For your reference. Where Com.sun.image is not a Java Standard package. Additional downloads are required. The relevant address is:

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

1. random.jsp (generates four-bit random characters, consisting of 0-9,a-z,a-z.) and put the final string in session to save for subsequent pages to verify authenticity.

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= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

Char display[]={' 0 ', ', ' 0 ', ', ' 0 ', ', ' 0 '},ran[]={' 0 ', ' 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 ();
Following fill background color
G.setcolor (Color.green);
G.fillrect (0, 0, width, height);
Set Font Color
G.setcolor (color.red);
g.DrawString (random,3,10);
G.dispose ();
Servletoutputstream OutStream = Response.getoutputstream ();
JPEGImageEncoder encoder =jpegcodec.createjpegencoder (OutStream);
Encoder.encode (image);
Outstream.close ();
%>

2. img.jsp (display verification picture.) Because this program is simple to let the verification program together.

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 code Error!") Please try again. ') </script> ");

Out.println ("<script>history.go ( -1) </script>");

Response.sendredirect ("img.jsp");
}
Else
{
Out.println ("<center> Verification success!") </center> ");
}
}
%>
<title> Photo Validation </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<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>

Description : Interested netizens can increase the content of the picture on this basis. For example, add texture to the background. Make the character position skew to increase the difficulty of the scanner. You have any views and suggestions, you can reply to the proposed ....



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.