JSP generated page verification code method [with Code]_jsp programming

Source: Internet
Author: User
Tags rand
image.jsp------------------------------A JSP page that generates random CAPTCHA pictures
The code is as follows:
Copy Code code 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=60, 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<155;i++)
{
int x = random.nextint (width);
int y = random.nextint (height);
int xl = Random.nextint (12);
int yl = Random.nextint (12);
G.drawline (X,y,x+xl,y+yl);
}
String srand= "";
for (int i=0;i<4;i++) {
String rand=string.valueof (Random.nextint (10));
Srand+=rand;
G.setcolor (New Color (20+random.nextint), 20+random.nextint (a), 20+random.nextint (110));
g.DrawString (rand,13*i+6,16);
}
Save authentication Code in session
Session.setattribute ("Rand", SRand);
G.dispose ();
Imageio.write (Image, "JPEG", Response.getoutputstream ());
%>

logic.jsp--------------------------------Landing page, provide input verification code here and then submit and verify
The code is as follows:
Copy Code code as follows:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page language= "java" import= "java.sql.*" errorpage= ""%>
<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= ><input "text" type= "Rand" Name= "size=" Td>
&LT;TD width= "138" valign= "Middle" align= "center" ></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= "Submission" > </td>
</tr>
</form>
</table>
</body>

Validate.jsp--------------------------used to verify that the input verification code is correct
The code is as follows:
Copy Code code as follows:

<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<%
String rand = (string) session.getattribute ("Rand");
String input = Request.getparameter ("Rand");
if (rand.equals (input)) {
Out.print ("<script>alert" (' Validation passed!) ');</script> ");
} else{
Out.print ("<script>alert" Please enter the correct authentication code!) '); location.href= ' login.jsp ';</script> ');
}
%>

The above three JSP pages are placed in and Web-inf sibling directory, if you put in a different folder, then the relevant pages within the path of their own a little change on it.
Friendship Hint: TOMCAT5 The following version will appear abnormal
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.