JSP Verification Code (digit + letter) __js

Source: Internet
Author: User
Tags rand
1. Establishment of IMAGE.JSP documents, as follows:
<%@ page contenttype= "Image/jpeg"
Import= "Java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%! Color getrandcolor (int fc, int BC) {
Random Random = new Random ();
if (FC > 255)
FC = 255;
if (BC > 255)
BC = 255;
int r = FC + Random.nextint (BC-FC);
int g = FC + Random.nextint (BC-FC);
int B = FC + Random.nextint (BC-FC);
return new Color (R, G, b);
}%>
<%
Out.clear ()//This sentence for the resin server, if it is tomacat can not this sentence
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);

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

Graphics g = image.getgraphics ();
Random Random = new Random ();

G.setcolor (Getrandcolor (200, 250));
G.fillrect (0, 0, width, height);

G.setfont (New Font ("Times New Roman", Font.plain, 18));

G.setcolor (Getrandcolor (160, 200));
for (int i = 0; i < i++) {
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (10);
int yl = Random.nextint (10);
G.drawline (x, y, X + xl, y + yl);
}

Char c[] = new char[62];

for (int i = 123, j = 0; I < i++, j + +) {
C[J] = (char) i;
}
for (int o = o++, p =; o <; p++) {
C[P] = (char) o;
}
for (int m =, n = m < m++, n++) {
C[n] = (char) m;
}
String SRand = "";
for (int i = 0; i < 4; i++) {
int x = Random.nextint (62);
String rand = string.valueof (c[x]);
SRand + rand;

G.setcolor (New Color (+ random.nextint), + random
. Nextint, + random.nextint (110));
g.DrawString (Rand, * i + 6, 16);
}

Save authentication Code in session
Session.setattribute ("Rand", SRand);
System.out.println (SRand);
G.dispose ();
Imageio.setusecache (TRUE);
Imageio.write (Image, "JPEG", Response.getoutputstream ());
%>

2. Establish the verification page validate.jsp, as follows
<%@ page language= "java" contenttype= "text/html; charset=gb2312 "
pageencoding= "GB2312"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Validation </title>
<body>
<%! String rand = NULL;

String input = null;%>
<%
Rand = (String) session.getattribute ("Rand");
input = Request.getparameter ("Yanzhengma");
%>
The Verification code is:
<%=rand%>
Your input is:
<%=input%>
<%
if (rand.equals (input)) {
%>
<font Color=green> entered the same, authentication succeeded. </font>
<%
} else {
%>
<font color=red> input is different, authentication failed. </font>
<%
}
%>
</body>


3. Set up the login page login.jsp as follows
<%@ page language= "java" import= "java.sql.*" errorpage= ""%>
<%
Request.setcharacterencoding ("GB2312");
%>
<% @page pageencoding= "GB2312"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> User Login </title>
<script language= "JavaScript" >
function LoadImage () {
document.getElementById ("Randimage"). src = "image.jsp?" +math.random ();
}
</script>

<body>

<table width= "256" border= "0" cellpadding= "0" cellspacing= "0" >
<!--dwlayouttable-->
<form action= "validate.jsp" method= "post" name= "LoginForm" >
<tr>
&LT;TD width= "118" height= "valign=" Middle "center" align=
Type= "text" name= "Yanzhengma" size= "></td>"
&LT;TD width= "138" valign= "Middle" align= "center" >Name= "Randimage" id= "Randimage" src= "image.jsp" width= "60"
height= "border=" "1" align= "Absmiddle" ></td>
</tr>
<tr>
&LT;TD height= "colspan=" "2" align= "center" valign= "Middle" ><a
Href= "Javascript:loadimage ();" ><font class=pt95> don't count me </font></a></td>
</tr>
<tr>
&LT;TD height= "colspan=" "2" align= "center" valign= "Middle" ><input
Type= "Submit" name= "Login" value= "submitted" ></td>
</tr>
</form>
</table>
</body>
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.