Login with Random Code

Source: Internet
Author: User

Image. jsp

<% @ 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 );
}
%>
<%
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );

Int I _width = 60;
Int I _height = 20;
Bufferedimage image = new bufferedimage (I _width, I _height, bufferedimage. type_int_rgb );

Graphics G = image. getgraphics ();

Random random = new random ();

G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, I _width, I _height );

G. setfont (new font ("Times New Roman", Font. Plain, 18 ));

G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++)
{
Int I _x = random. nextint (I _width );
Int I _y = random. nextint (I _height );
Int I _xl = random. nextint (12 );
Int I _yl = random. nextint (12 );
G. drawline (I _x, I _y, I _x + I _xl, I _y + I _yl );
}

String s_rand = "";
For (INT I = 0; I <4; I ++)
{
String Rand = string. valueof (random. nextint (10 ));
S_rand + = rand;

G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}

Session. setattribute ("RAND", s_rand );

G. Dispose ();
 
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
%>
Use image. jsp

Login. jsp

<Form name = "formadv" Action = "../advhost/login_do.jsp" "method =" Post "target =" _ Self "onsubmit =" Return docheck () ">
<Table>
<Tr>
<TD> User name: </TD>
<TD> <input type = "text" name = "username" size = 15>
</TD>
<TD> password: </TD>
<TD> <input type = "password" name = "password" size = 15> </TD>
<TD> Verification Code: </TD>
<TD> <input type = "text" name = "RAND" size = 5> & nbsp; </TD>

<TD> <input type = "Submit" value = "Submit" size = 15> </TD>
<TD> <input type = "button" value = "cancel" size = 15> </TD>
</Tr>
</Table>
</Form>

Login_do.jsp

string imgrand = (string) session. getattribute ("RAND");
If (Rand. equals (imgrand) {
try {
dB. creatconnection ();
string SQL = "select * From adshost where username = '" + username + "' and Password = '" + password + "'";
// string SQL = "select * From websitehost where username = 'facepp' and Password = 911";
system. out. println (SQL);
resultset rs=db.exe cutequery (SQL);
If (RS. next () {

response. sendredirect (".. /advhost/index. JSP ");
}< br> else {
// system. out. println ("You are wrong");
}< br> // while (RS. next () {out. println (RS. getstring (2) ;}< br> dB. closeconnection ();
Rs. close ();
}< br> catch (exception e) {system. out. println (E. tostring () ;}< BR >}< br> else {
out. print ("the verification code you entered is incorrect! ");
out. println (" ");
}

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.