Jsp login verification code

Source: Internet
Author: User

------------------------------------------------------

Image. jsp

-----------------------------------------------------

<% @ Page language = "java" import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio. * "pageEncoding =" UTF-8 "%>
<%
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>
<Base href = "<% = basePath %>">

<Title> My JSP 'images. jsp 'starting page </title>

<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->

</Head>

<Body>
<%!
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 );
}
%>
<%
Out. Clear ();
// 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 ("Comic Sans MS", Font. Plain, 18 ));

// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
G. setcolor (getrandcolor (220,240 ));
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 of the called function is the same. It may be because the seed is too close and can only be directly generated.
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 ());
%>

 

</Body>
</Html>

 

-----------------------------

Code extracted from login. jsp

----------------------------

Function loadimage (){
Document. getelementbyid ("randimage"). src = "images. jsp? "+ Math. Random ();
}

 

<Tr>
<TD width = "20%" Height = "15"> <Div align = "right"> <SPAN class = "style1"> Verification Code </span> </div> </ TD>
<TD width = "27%" Height = "15"> <Div align = "center">
& Nbsp; <input type = "text" name = "RAND" id = "RAND" style = "width: 55px; Height: 17px; Background-color: # ffffff; Border: solid 1px #7dbad7; font-size: 12px; color: # 2087bf "/>
</Div> </td>
<Td colspan = "2" width = "57%" height = "15"> & nbsp; </td>
</Tr>
<Tr>
<Td height = "10" colspan = "4" align = "center"> <a href = "javascript: loadimage (); "> <span class =" STYLE1 "> <font color =" red "> you cannot see it clearly. Change it to another one. </font> </span> </a> </td>
</Tr>

<C: if test = "$ {imageResult = 1}">
<Span class = "STYLE1"> <font color = "red"> the verification code is incorrect. Enter the verification code again. </Font> </span>
</C: if>

------------------------------------

KsuserAction. java

-----------------------------------

String rand = request. getParameter ("rand ");
String imageRand = (String) request. getSession (). getAttribute ("rand ");

If (rand. equals (imageRand )){
ImageResult =-1;
Return Default ();
} Else {
ImageResult = 1;
Return Login ();
}

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.