Image. jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "%>
<%!
Color getRandColor (int fc, int bc) {// obtain a random Color from a given range
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 );
}
%>
<%
// Set the page not to cache
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// Create an image in memory
Int width = 60, height = 20;
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Generate a random class
Random random = new Random ();
// Set the background color
G. setColor (getRandColor (200,250 ));
G. fillRect (0, 0, width, height );
// Set the font
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18 ));
// Draw a border
// G. setColor (new Color ());
// G. drawRect (0, 0, width-1, height-1 );
// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
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 );
}
// Obtain the random ID code (4 digits)
String sRand = "";
For (int I = 0; I <4; I ++ ){
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
// Display the authentication code to the image
G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110); // The color from the call function is the same, probably because the seed is too close, so it can only be generated directly.
G. drawString (rand, 13 * I + 6, 16 );
}
// Save the authentication code to the SESSION
Session. setAttribute ("rand", sRand );
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
%>
A. jsp labels ------------------------------------------------------------------------------------------------------
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + "://"
+ Request. getServerName () + ":" + request. getServerPort ()
+ Path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> certificate code input page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<META HTTP-EQUIV = "Pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache">
<META HTTP-EQUIV = "Expires" CONTENT = "0">
</Head>
<Body>
<Form method = post action = "check. jsp">
<Table>
<Tr>
<Td align = left>
The verification code generated by the system:
</Td>
<Td>
</Td>
</Tr>
<Tr>
<Td align = left>
Enter the above verification code:
</Td>
<Td>
<Input type = text name = rand maxlength = 4 value = "">
</Td>
</Tr>
<Tr>
<Td colspan = 2 align = center>
<Input type = submit value = "submit detection">
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
Check. jsp restart ----------------------------------------------------------------------------------------------------------------------
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<% @ Page contentType = "text/html; charset = gb2312" language = "java"
Import = "java. SQL. *" errorPage = "" %>
<Html>
<Head>
<Title> verification page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<META HTTP-EQUIV = "Pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache">
<META HTTP-EQUIV = "Expires" CONTENT = "0">
</Head>
<Body>
<%
String rand = (String) session. getAttribute ("rand ");
String input = request. getParameter ("rand ");
%>
The verification code generated by the system is:
<% = Rand %>
<Br>
The verification code you entered is:
<% = Input %>
<Br>
<Br>
<%
If (rand. equals (input )){
%>
<Font color = green> the input is the same and the authentication is successful! </Font>
<%
} Else {
%>
<Font color = red> If the input is different, authentication fails! </Font>
<%
}
%>
</Body>
</Html>