Next we will start to create a message page!
<% @ Page
Import = "Java. util .*"
Import = "Java. Text .*"
Import = "Java. SQL .*"
Import = "Java. Io .*"
Import = "Java. Lang .*"
Contenttype = "text/html; charset = gb2312"
%>
<%
Class commenterror // throws java. Lang. nullpointerexception
{Public String username = "", sex = "", address = "", postal = "", OICQ = "", ICQ = "", tel = "", comment = "";
Public Boolean noerror = true; // false;
Public int errorcount = 0;
Private Boolean isnumber (string S1 ){}
Public String font (string SE ){}
Public String comment_er (string SE ){}
Public void username (string SE ){}
Public void sex (string SE ){}
Public void address (string SE ){}
Public void comment (string SE ){}
Public void Tel (string SE ){}
Public void postal (string SE ){}
Public void OICQ (string SE ){}
Public void ICQ (string SE ){}
}
Class formatcomment
{Public String Replace (string source, string oldstring, string newstring ){}
Public String formatint (string SE ){}
Public String fromatcomment (string SE ){}
Public String tohtmlinput (string Str ){}
Public String tohtml (string Str ){}
Public String tosql (string Str) {}// convert to the format that can be added to myqal
}
%>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Meta name = "generator" content = "Microsoft FrontPage 4.0">
<Meta name = "progid" content = "FrontPage. Editor. Document">
<Title> use a MySQL database </title>
</Head>
<Body>
<P align = "center"> <center> <font style = "color: Blue; font-family: 正, 文,, fon; font-size: 16pt "> Ping Hui JSP message board </font> </center> </P>
<P align = "center">
<%! String username, sex, address, post, OICQ, ICQ, telnumber, comment, email, URL. urltitle;
%>
<%
Try {username = request. getparameter ("name ");
} Catch (nullpointerexception e) {username = "";}
Try {comment = request. getparameter ("comment ");
} Catch (nullpointerexception e) {comment = "";}
Try {sex = request. getparameter ("sex ");
} Catch (nullpointerexception e) {sex = "";}
Try {address = request. getparameter ("Address ");
} Catch (nullpointerexception e) {address = "";}
Try {post = request. getparameter ("Postal ");
} Catch (nullpointerexception e) {post = "";}
Try {OICQ = request. getparameter ("OICQ ");
} Catch (nullpointerexception e) {OICQ = "";}
Try {ICQ = request. getparameter ("ICQ ");
} Catch (nullpointerexception e) {ICQ = "";}
Try {telnumber = request. getparameter ("telphone ");
} Catch (nullpointerexception e) {telnumber = "";}
Try {email = request. getparameter ("email ");
} Catch (nullpointerexception e) {email = "";}
Try {url = request. getparameter ("url ");
} Catch (nullpointerexception e) {url = "";}
Try {urltitle = request. getparameter ("urltitle ");
} Catch (nullpointerexception e) {urltitle = "";}
String IP = request. getremoteaddr (); // obtain the IP address
String time = (New simpledateformat ("yyyy-mm-dd hh: mm: SS", locale. US). Format (New java. util. Date ());
String userid = "";
String myquery = "";
Here, I call a class that checks the message validity.
Commenterror testcomment = new commenterror ();
Testcomment. username (username );
Testcomment. Postal (post );
Testcomment. Sex (sex );
Testcomment. Address (Address );
Testcomment. Tel (telnumber );
Testcomment. Comment (comment );
Testcomment. OICQ (OICQ );
Testcomment. ICQ (ICQ );
If (testcomment. noerror) // there is no error in the message, write to the database
Try {// The database is successfully written.
Java. SQL. Connection sqlconn; // database connection object
Java. SQL. Statement sqlstmt; // statement object
Java. SQL. resultset sqlrst; // result set object
// Register the JDBC driver object
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
// Connect to the database
Sqlconn = java. SQL. drivermanager. getconnection ("JDBC: mysql: // localhost/pinghui", "test ","");
// Create a statement object
Sqlstmt = sqlconn. createstatement (Java. SQL. resultset. type_scroll_insensitive, java. SQL. resultset. concur_read_only );
// Execute the SQL statement
Here, a class is called to process messages, which can be recognized by MySQL,
Formatcomment fc = new formatcomment ();
OICQ = FC. formatint (OICQ );
ICQ = FC. formatint (ICQ );
Post = FC. formatint (post );
Telnumber = FC. formatint (telnumber );
Username = FC. tosql (username );
Comment = FC. tosql (comment );
Myquery = "insert into comment (username, sex, address, IP, post, OICQ, ICQ, telnumber, comment, time, URL, email) values ('"+ username +"', '"+ sex +"', '"+ address +"', '"+ IP +"', "+ post + ", "+ OICQ +", "+ ICQ +", '"+ telnumber +"', '"+ comment +"', now (), '"+ URL + "', '"+ email + "');";
Sqlrst=sqlstmt.exe cutequery (myquery); // Add data to the database
Sqlrst. Close (); // close the result set object
Sqlstmt. Close (); // close the statement object
Sqlconn. Close (); // close the database connection
Out. Print (time );
%>
<Font size = "5" face = "文 "> the message is sent successfully. Thank you! </Font>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "100%">
<Tbody>
<Tr>
<TD width = "25%"> nickname: <% = username %> </TD>
<TD width = "25%"> Gender: <% = sex %> </TD>
<TD colspan = "2" width = "50%"> address: <% = address %> </TD>
</Tr>
<Tr>
<TD width = "25%"> Tel: <% = telnumber %> </TD>
<TD width = "25%"> zip code: <% = post %> </TD>
<TD width = "25%"> OICQ: <% = OICQ %> </TD>
<TD width = "25%"> ICQ: <% = ICQ %> </TD>
</Tr>
<Tr>
<TD colspan = "2" width = "50%"> Email: <a href = "mailto:" Title = ""> </a> </TD>
<TD colspan = "2" width = "50%"> URL: <a href target = "_ blank"> </a> </TD>
</Tr>
<Tr>
<TD colspan = "4"> <font style = "color: green; line-Height: 150%"> message: </font> <% = comment %> <font style = "color: green; line-width: 150%"> <br>
-</Font> <% = time %> <font style = "color: green; line-height: 150% "> (from </font> <% = IP %> <font style =" color: green; line-Height: 150% ">) </font> </TD>
</Tr>
</Tbody>
</Table>
<HR color = "#0080c0" noshade size = "1" width = "100%">
<%
} Catch (sqlexception e) // failed to write to the database
{Out. Print ("<font color = Red> Message failed </font> ");
Out. Print (myquery );
Out. Print (userid );
}
Else
{
Out. Print ("<font color = Red> total" + testcomment. errorcount + "error data, Please modify it! </Font> ");
%>
<Script language = "JavaScript">
<! --
Function test_comments (theform)
{
If (theform. Name. value = "")
{Alert ("name cannot be blank! :-)");
Theform. Name. Focus ();
Return (false );
}
If (theform. Name. value. length> 12)
{Alert ("the name is too long and cannot contain more than 12 characters (six Chinese characters )! ");
Theform. Name. Focus ();
Return (false );
}
If (theform. Address. value. length> 40)
{Alert ("the address is too long and cannot exceed 40 characters (20 Chinese characters )! ");
Theform. Name. Focus ();
Return (false );
}
If (theform. Comment. value = "")
{Alert ("sorry, the message cannot be blank! You must enter a message ");
Theform. Comment. Focus ();
Return (false );
}
If (theform. Comment. value. length> 1000)
{Alert ("sorry, your message is too long. It can contain a maximum of 1000 characters! ");
Theform. Comment. Focus ();
Return (false );
}
Return (true );
} -->
<Table>
<Tr>
<TD>
<Table cellspacing = "0" cellpadding = "0">
<Tr>
<TD> <! -- Form start -->
<Form action = "addmysql. jsp" method = "Post" name = "all_comments" onsubmit = "javascript: Return (test_comments (this)">
<Table border = "0" width = "100%" cellspacing = "0" cellpadding = "0">
<Tr>
<TD width = "60%"> nickname: <input name = "name" size = "20" value = "<% IF (username! = NULL) Out. Print (username); %> "> <% = testcomment. comment_er (" username ") %> </TD>
<TD nowrap width = "40%"> sex: <select name = "sex" size = "1">
<Option selected value = "male" <% IF (sex = "male") Out. Print ("select"); %> male </option>
<Option value = "" <% IF (sex = "") Out. Print ("select"); %> female </option>
<Option value = "confidential" <% IF (sex = "confidential") Out. Print ("select"); %> confidential </option>
</SELECT> <% = testcomment. comment_er ("sex") %> </TD>
</Tr>
<Tr>
<TD colspan = "2"> address: <input name = "Address" size = "40" value = "<% IF (address! = NULL) Out. Print (Address); %> "> <% = testcomment. comment_er (" Address ") %> </TD>
</Tr>
<Tr>
<TD> Tel: <input name = "telphone" size = "15" value = "<% IF (telnumber! = NULL) Out. Print (telnumber); %> "> <% = testcomment. comment_er (" tel ") %> </TD>
<TD> post: <input name = "Postal" size = "6" value = "<% IF (post! = NULL) Out. Print (post); %> "> <% = testcomment. comment_er (" Postal ") %> </TD>
</Tr>
<Tr>
<TD> OICQ: <input name = "OICQ" size = "10" value = "<% IF (OICQ! = NULL) Out. Print (OICQ); %> "> <% = testcomment. comment_er (" OICQ ") %> </TD>
<TD> ICQ: <input name = "ICQ" size = "15" value = "<% IF (ICQ! = NULL) Out. Print (ICQ); %> "> <% = testcomment. comment_er (" ICQ ") %> </TD>
</Tr>
<Tr>
<TD colspan = "2"> Email: <input name = "email" size = "25" value = "<% IF (email! = NULL) Out. Print (email); %> "> <% // = testcomment. comment_er (" ") %> </TD>
</Tr>
<Tr>
<TD colspan = "2"> address: <input name = "url" size = "40" value = "<% IF (URL! = NULL) Out. Print (URL); %> "> <% // = testcomment. comment_er (" ") %> </TD>
</Tr>
<Tr>
<TD colspan = "2"> <textarea Cols = "80" name = "comment" rows = "10" Wrap = "physical"> <% IF (comment! = NULL) Out. Print (comment); %> </textarea>
<Tr>
<TD align = "right"> <input name = "put_submit" type = "Submit" value = "OK"?> </TD>
<TD align = "Left"> <input name = "R" type = "reset" value = "reset"> </TD>
</Tr>
</Table>
</Form>
<! -- End of the submission form -->
</TD>
<TD> <% = testcomment. comment_er ("comment") %> note: In the entered content, the nickname and message are required items! <Br>
HTML flag not supported </TD>
</Tr>
</Table>
</TD>
</Tr>
</Table>
<%
}
%>
</Body>