Verification code for the mxnenews system (including five files)
Function. Java
Verifycode. Java
Verifycode. jsp
Usage page, processing page)
1. Page for verification code
Verification Code:
<Input name = "verifycode" type = "text" class = "Chinese" id = "verifycode" style = "font-size: 12px" size = "6"
Maxlength = "6">
<! -- Error message -->
<% = (String) Session. getattribute ("error") = NULL )? "": (String) Session. getattribute ("error") %>
2. Processing page
String scode = function. checkreplace (request. getparameter ("verifycode "));
If (! Scode. Equals (session. getattribute ("verifycode ")))
{
Session. setattribute ("error", "<li> Verification code error ");
Response. sendredirect ("index. jsp"); // return to the logon page
Return;
}
3. The source code is provided below for learning and researching verifycode. jsp.
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. AWT. *, java. AWT. image. *" %>
<% @ Page import = "Java. util. *, javax. ImageIO. *, java. Io. *" %>
<% @ Page import = "dreamtime. dreamnews. verifycode" %>
<%! String sverifycode; // verification code string %>
<% // Set that the page is not cached
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. Reset ();
Try {
// Save the authentication code to the session
// Call runverifycode (int I) and change I to the number of digits of the Verification Code
Verifycode Vc = new verifycode ();
Session. setattribute ("verifycode", VC. runverifycode (4 ));
// Session. setmaxinactiveinterval (60 );
// Output the image to the page
Sverifycode = (string) Session. getattribute ("verifycode ");
Outputstream outs = response. getoutputstream ();
ImageIO. Write (VC. createimage (sverifycode), "Jpeg", outs );
} Catch (exception e ){
Return;
}
%>
4. verifycode. Java
/*************************************** ************
*
* Source File Name: verifycode. Java
* Success: Dream news system-Verification Code
* Author: dreamtime
* Email: fanwsp@126.com
* QQ: 122142023
* Copyright (c) 2005-2006 by dreamtime
*
**************************************** ************
*/
Package dreamtime. dreamnews; // package where the specified class is located
Import java. AWT. *; // import class
Import java. AWT. image .*;
Import java. util .*;
Import javax. ImageIO .*;
// Define a class
Public class verifycode {
Static random r = new random ();
Static string ssource = "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz" +
"0123456789 ";
Static char [] src = ssource. tochararray ();
// Generate a random string
Private Static string randstring (INT length ){
Char [] Buf = new char [length];
Int RND;
For (INT I = 0; I <length; I ++ ){
RND = math. Abs (R. nextint () % SRC. length;
Buf [I] = SRC [RND];
}
Return new string (BUF );
}
// Call this method to generate a random string,
// Parameter I: String Length
Public String runverifycode (int I ){
String verifycode = randstring (I );
Return verifycode;
}
// Obtain the random color in a given range
Public 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 );
}
// Call this method to generate an image for the obtained Verification Code
// Scode: Pass the verification code W: Image Width H: Image Height
Public bufferedimage createimage (string scode)
{
Try {
// Character Font
Font codefont = new font ("Arial black", Font. Plain, 16 );
Int ilength = scode. Length (); // obtain the verification code length.
Int width = 22 * ilength, Height = 20; // image width and height
Int charwidth = (INT) (width-24)/ilength; // The width of the character from the left
Int charheight = 16; // character distance
Edge height
// Create an image in memory
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,240 ));
G. fillrect (0, 0, width, height );
// Set the font
G. setfont (codefont );
// Draw a border with random colors
G. setcolor (getrandcolor (10, 50 ));
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 );
}
For (INT I = 0; I <ilength; I ++)
{
String Rand = scode. substring (I, I + 1 );
// Display the authentication code to the image
G. setcolor (new color (20 + random. nextint (60), 20 + random. nextint
(120), 20 + random. nextint (180 )));
G. drawstring (RAND, charwidth * I + 14, charheight );
}
// The image takes effect
G. Dispose ();
Return image;
} Catch (exception e ){
// E. printstacktrace ();
// System. Out. println (E. getmessage ());
}
Return NULL;
}
// Test
Public static void main (string [] ARGs ){
// Verifycode Vc = new verifycode ();
// String S1 = VC. runverifycode (4 );
// Fun. dreamnewstitle; system. Out. println (S1 );
// Image im = VC. createimage (S1 );
// Graphics G = Im. getgraphics ();
// G. drawimage (IM, 20, 20, this );
// G. drawstring (S1, 20, 20 );
}
}
5. function. Java
Import java. SQL .*;
Import java. util. calendar;
Public class function {
/*************************************** ************************************
* Function name: checkreplace used for conversion SQL special string parameter: S: string type, character return value to be converted: converted string
* Use: String S2 = checkreplace (S1 );
**************************************** **********************************/
Public static string checkreplace (string s ){
Try {
If (S = NULL | S. Equals (""))
Return "";
Else {
Stringbuffer = new stringbuffer ();
For (INT I = 0; I <S. Length (); I ++ ){
Char c = S. charat (I );
Switch (c ){
Case 34: // & apos; "& apos;
Stringbuffer. append ("& quot ;");
Break;
Case 39: // & apos;/& apos;
Stringbuffer. append ("& #039 ;");
Break;
Case 124: // & apos; | & apos;
Stringbuffer. append ("");
Break;
Case & apos ;:
Stringbuffer. append ("& amp ;");
Break;
Case & apos; <& apos ;:
Stringbuffer. append ("& lt ;");
Break;
Case & apos;> & apos ;:
Stringbuffer. append ("& gt ;");
Break;
Default:
Stringbuffer. append (C );
Break;
}
}
Return stringbuffer. tostring (). Trim (); // return the converted string
}
} Catch (exception e ){
Return "";
}
}
}
Appendix:
Background Management instructions:
Address: For admin/index. jsp, you must enter the file name index. jsp. Otherwise, you cannot log on.
Because string page1 = (string) request. getheader ("Referer"); // obtain the page Address Source
String page2 = request. getrequesturl (). tostring (); // obtain the current page address.
If (page2.equals (page1) // you cannot directly log on via http: // localhost/admin. You must use http:/index. jsp to log on.
Username: Admin
Password: Admin (74d839d98630e280df752e8939454a6b)
Appendix 2: Click to bring up the verification code, which is used for the message. When the message box is activated, the verification code is issued, saving the network speed.
<Div id = "verify" style = "display: none;">
</Div>
<Script language = "JavaScript">
Function creatediv ()
{
Document. getelementbyid ("verify"). style. Display = "Block ";
Document. getelementbyid ("verify"). innerhtml = " ";
}
</SCRIPT>
<Input name = "verifycode" type = "text" class = "Chinese" id = "verifycode" style = "font-size: 12px "size =" 6 "maxlength =" 6"
Onclick = "creatediv ();";
Onfocus = "creatediv ();"
Onblur = "document. getelementbyid ('verify '). style. Display = 'none ';"
/>