Jsp message board source code 2: for jsp beginners.

Source: Internet
Author: User

Prepared by: precom (pig) 2000.12.10

Guestbook. jsp
======================================
<Html> <META content = "text/html; charset = gb2312" http-equiv = Content-Type>

<Title> Zhangjiajie Yellow Pages (114 on the Internet) </title> <Style type = "text/css">
<! --
BODY {FONT-FAMILY: "", "Arial Narrow", "Times New Roman"; FONT-SI
ZE: 9pt}
. P1 {FONT-FAMILY: "", "Arial Narrow", "Times New Roman"; FONT-SIZ
E: 12pt}
A: link {COLOR: # 00793d; TEXT-DECORATION: none}
A: visited {TEXT-DECORATION: none}
A: hover {TEXT-DECORATION: underline}
TD {FONT-FAMILY: "", "Arial Narrow", "Times New Roman"; FONT-SIZE
: 9pt}
. P2 {FONT-FAMILY: "", "Arial Narrow", "Times New Roman"; FONT-SIZ
E: 9pt; LINE-HEIGHT: 150%}
. P3 {FONT-FAMILY: "", "Arial Narrow", "Times New Roman"; FONT-SIZ
E: 9pt; LINE-HEIGHT: 120%}

-->
</Style>

<Body>

<% @ Page contentType = "text/html; charset = GB2312" %>
<% @ Page language = "java" import = "java. SQL. *" %>
<Jsp: useBean id = "testInq" scope = "page" class = "ymbean. opDb"/>
<%
Int pages = 1;
Int pagesize = 10;
Int count = 0;
Int totalpages = 0;

String countsql = "", inqsql = "", lwhere = "", insertsql = "", st = "";

String lw_title = "", lw_author = ", pagetitle =" ", author_http =" ", author_e
Mail = "", lw_ico = "",
Lw_content = "", lw_class1 = "";
String author_ip = "", lw_time = "", lw_class2 = "", lw_type = "", zt_time = "", zt
_ Author = "";
Int answer_num = 0, click_num = 0;
Int inquire_item = 1;
String inquire_itemt = "", inquire_value = "";
String lurlt = "<a href = guestbook. jsp? ", Llink = "";

Lwhere = "where lw_type = 'Z'"; // only show the primary sticker

/*
Enumeration e = request. getParameterNames ();
While (e. hasMoreElements ()){
String name = (String) e. nextElement ();
*/
Try {
// Obtain the order number of the displayed pages
Pages = new Integer (request. getParameter ("pages"). intValue ();
} Catch (Exception e ){}
Try {
// Retrieve query parameters
Inquire_item = new Integer (request. getParameter ("range"). intValu
E ();
Inquire_value = new String (request. getParameter ("findstr"). getByt
Es ("ISO8859_1 "));
If (inquire_item = 0) inquire_itemt = "lw_title ";
Else if (inquire_item = 1) inquire_itemt = "lw_content ";
Else if (inquire_item = 2) inquire_itemt = "lw_author ";
Else if (inquire_item = 3) inquire_itemt = "lw_time ";
Else if (inquire_item = 4) inquire_itemt = "lw_title ";
Lwhere = lwhere + "and" + inquire_itemt + "like '%" + inquire_value + "%
'";
Lurlt = lurlt + "range =" + inquire_item + "& findstr =" + inquire_value + "&"

} Catch (Exception e ){}

Try {
// Get parameter message content
Lw_class1 = new String (request. getParameter ("gbname"). getBytes ("ISO885
9_1 "));
Lw_title = new String (request. getParameter ("lw_title"). getBytes ("ISO88
59_1 "));
Lw_author = new String (request. getParameter ("lw_author"). getBytes ("ISO
8859_1 "));
Pagetitle = new String (request. getParameter ("pagetitle"). getBytes ("ISO
8859_1 "));
Author_http = new String (request. getParameter ("author_http"). getBytes (
"ISO8859_1 "));
Author_email = new String (request. getParameter ("author_email"). getByte
S ("ISO8859_1 "));
Lw_ico = request. getParameter ("gifface ");
Lw_content = new String (request. getParameter ("lw_content"). getBytes ("I
SO8859_1 "));
String requestMethod = request. getMethod ();
RequestMethod = requestMethod. toUpperCase ();
If (requestMethod. indexOf ("POST") <0)
{Out. print ("illegal operation! ");
Return;
}

// Form other data items
Author_ip = request. getRemoteAddr ();
Lw_time = testInq. getCurrentDate ("yyyyMMddHHmmss ");
Lw_class2 = "2 ";
Lw_type = "" + "z"; // post
Zt_time = lw_time;
Zt_author = lw_author;
Answer_num = 0;
Click_num = 0;
// ==========================
St = "','";
// Ensure that the length of all data items in the message is within the normal range
If (lw_title.length ()> 50) lw_title = lw_title.substring (0, 50 );
If (lw_author.length ()> 20) lw_author = lw_author.substring (0, 20 );
If (author_http.length ()> 40) author_http = author_http.substring (0, 40 );

If (author_email.length ()> 50) author_email = author_email.substring (0, 4
0 );
If (lw_content.length ()> 4000) lw_content = lw_content.substring)

Insertsql = "insert into guestbook values ('" + lw_title + st + lw_author + st +
Author_http + st +
Author_email + st + lw_ico + st + lw_time + "'," + answer_num + "," + cli
Ck_num + ", '" +
Author_ip + st + lw_class1 + st + lw_class2 + st + lw_type + st + zt_time
+ St + zt_author + st +
Lw_content + "')";
// Out. print (insertsql );
// Insert a message
Try {
String lmsg=testInq.exe cuteUpdate (insertsql );
If (lmsg. indexOf ("executeUpdate OK") <0)
Out. print ("lmsg =" + lmsg );

} Catch (Exception e) {out. print ("error:" + e );}
} Catch (Exception e ){}
%>

<%
// Verify the validity of the entered message.
String ljs = "<SCRIPT language = JavaScript> \ n" +
"<! -- \ N "+
"Function ValidInput () \ n" +
"{If (document. sign. lw_author.value = \" \ ") \ n" +
"{Alert (\" Please fill in your name. \ "); \ N" +
"Document. sign. lw_author.focus (); \ n" +
"Return false;} \ n" +
"If (document. sign. lw_title.value = \" \ ") \ n" +
"{Alert (\" Enter the message subject. \ "); \ N" +
"Document. sign. lw_title.focus (); \ n" +
"Return false;} \ n" +
"If (document. sign. author_email.value! = \ "\") \ N "+
"{If (document. sign. author_email.value.indexOf (\" @ \ ") <0) // (document
. Sign. author_email.value.indexOf (\":\")! =-1) \ n "+
"{Alert (\" the EMail you entered is invalid. Please enter a valid Email! \ "); \ N" +
"Document. sign. author_emaill.focus (); \ n" +
"Return false; \ n" +
"} \ N" +
"} \ N" +
"Return true; \ n" +
"} \ N" +
"Function ValidSearch () \ n" +
"{If (document. frmsearch. findstr. value = \" \ ") \ n" +
"{Alert (\" cannot search for empty strings! \ "); \ N" +
"Document. frmsearch. findstr. focus (); \ n" +
"Return false;} \ n" +
"} \ N" +
"// --> \ N" +
"</SCRIPT> ";
Out. print (ljs );
%>

<%
// Message board interface Header
String ltop = "<DIV align = center> \ n" +
"<CENTER> \ n" +
"<FORM action = guestbook. jsp method = post name = frmsearch> \ n" +
"<INPUT name = gbname type = hidden value = cnzjj_gt> \ n" +
"<TABLE align = center border = 0 cellSpacing = 1 width = \" 95% \ "> \ n" +
"<TBODY> \ n" +
"<TR> \ n" +
"<TD bgColor = #336699 colSpan = 2 width = \" 100% \ "> \ n" +
"<P align = center> <FONT color = # ffffff face = _ GB2312 \ n" +
"Size = 5> welcome friends from afar to Zhangjiajie for sightseeing </FONT> </P> </TD> </TR> \ n"
+
"<TR bgColor = # 6699cc> \ n" +
"<TD align = left noWrap width = \" 50% \ "> Home Page: <A \ n" +
"Href = \" http://www.zj.hn.cn \ "target = _ blank> <FONT \ n" +
"Color = # ffffff> Zhangjiajie tourism </FONT> </A> administrator: <A \ n"
+
"Href = \" mailto: dzx@mail.zj.hn.cninfo.net \ "> <FONT color = # ffffff>
Yi min </FONT> </A> \ n "+
">>< A \ n" +
"Href = \" http://www.zj.hn.cn \ "> <FONT \ n" +
"Color = # ffffff> management </FONT> </A> <A \ n" +
"Href = \" http://www.zj.hn.cn \ "> <FONT \ n" +
"Color = # ffffff> application </FONT> </A> </TD> \ n" +
"<TD align = right width = \" 50% \ "> <SELECT class = ourfont name = range s
Ize = 1> \ n "+
"<OPTION selected value = 0> by topic </OPTION> <OPTION value = 1> by internal
Capacity </OPTION> \ n "+
"<OPTION value = 2> by author </OPTION> <OPTION value = 3> by date </OPTI
ON> <OPTION \ n "+
"Value = 4> by topic & content </OPTION> </SELECT> <INPUT name = findst
R> <INPUT name = search onclick = \ "return ValidSearch () \" type = submit val
Ue = \ "Search \"> \ n "+
"</TD> </TR> </TBODY> </TABLE> </FORM> \ n" +
"<HR align = center noShade SIZE = 1 width = \" 95% \ "> \ n" +
"</CENTER> </div> ";
Out. print (ltop );
%>

<%
// Display a message posted last time
Countsql = "select count (lw_title) from guestbook" + lwhere;
Inqsql = "select lw_title, answer_num, click_num, lw_author, lw_time, expres
Sion, "+
"Author_email, lw_class1, lw_class2 from guestbook" + lwhere + "o
Rder by lw_time desc ";

If (pages> 0)
{
Try {
Try {
ResultSet rcount=testInq.exe cuteQuery (countsql );
If (rcount. next ())
{
Count = rcount. getInt (1 );
}
Rcount. close ();
} Catch (Exception el1) {out. println ("count record error
: "+ El1 +" <br> ");
Out. println (countsql );
}

Totalpages = (int) (count/pagesize );
If (count> totalpages * pagesize) totalpages ++;
St = "" +
"<TABLE align = center border = 0 cellPadding = 0 cellSpacing =
0 width = \ "95% \" type = "codeph" text = "codeph"> "+
"<TBODY> <TR> <TD align = middle bgColor = #97 badd width = \" 1
00% \ "> <FONT color = # ff0000>" +
"Total" + totalpages + "page," + count +"
. "+" Current page: "+ pages +
"</FONT> </TD> </TR> </TBODY> </TABLE> <BR> ";

Out. print (st );

// Out. print ("Total" + totalpages + "page," +
Count + "entries." + "Current page:" + pages + "<br> ");

St = "<center>" +
& Quot; <TABLE border = 0 cellPadding = 2 cellSpacing = 1 width = \ & quot; 95% \ & quot;> & quot; +
"<TBODY>" +
"<TR>" +
"<TD align = middle bgColor = # 6699cc width = \" 55% \ "> <FONT" +
"Color = # ffffff> Message Subject </FONT> </TD>" +
"<TD align = middle bgColor = # 6699cc width = 50> <FONT" +
"Color = # ffffff> Response count </FONT> </TD>" +
"<TD align = middle bgColor = # 6699cc width = 40> <FONT" +
"Color = # ffffff> clicks </FONT> </TD>" +
"<TD align = middle bgColor = # 6699cc width = 100> <FONT" +
"Color = # ffffff> author name </FONT> </TD>" +
"<TD align = middle bgColor = # 6699cc width = 140> <FONT" +
"Color = # ffffff> posting/response time </FONT> </TD> </TR> ";
Out. print (st );

If (count> 0)
{
ResultSet rs = testInq.exe cuteQuery (inqsql );
ResultSetMetaData metaData = rs. getMetaData ();

Int I;
// Skip pages-1 to point cursor to pages and prepare to display
For (I = 1; I <= (pages-1) * pagesize; I ++) rs. next ();
// Display the beginning of pages
String linestr = "";
For (I = 1; I <= pagesize; I ++)
If (rs. next ())
{
Lw_title = rs. getString ("lw_title ");
Answer_num = rs. getInt ("answer_num ");
Click_num = rs. getInt ("click_num ");
Lw_author = rs. getString ("lw_author ");
Lw_time = rs. getString ("lw_time ");
St = lw_time.substring (0, 4) + "-" + lw_time.substring (4, 6) + "-"
+ Lw_time.substring (6, 8) + ":" +
Lw_time.substring (8, 10) + ":" + lw_time.substring (10, 12) +
":" + Lw_time.substring (12, 14 );
Lw_ico = rs. getString ("expression ");
Author_email = rs. getString ("author_email ");
Lw_class1 = rs. getString ("lw_class1 ");
Lw_class2 = rs. getString ("lw_class2 ");
Llink = "reply. jsp? Lw_class1 = "+ lw_class1 +" & lw_class2 = "+ lw _
Class2 + "& zt_time =" + lw_time +
"& Zt_author =" + author_email;
Linestr = "<TR bgColor = # d5e8fd> \ n" +
"<TD bgColor = # d5e8fd> <IMG src = \" "+ lw_ico +". gif \
"> <A" +
"Href = \" "+ llink +" \ ">" + lw_title + "</A> </TD>" +
"<TD align = middle> [" + answer_num + "] </TD>" +
"<TD align = middle>" + click_num + "</TD>" +
"<TD align = middle> <A href = \" mailto: "+ author_ema
Il + "\"> "+ lw_author +" </A> </TD> "+
"<TD align = middle>" + st + "</TD> </TR> ";

Out. println (linestr );

}
Rs. close ();
// Display page pages to end
St = "</TBODY> </TABLE> <BR> ";
Out. print (st );

Int iFirst = 1, iLast = totalpages, iPre, iNext;
If (pages <= 1) iPre = 1;
Else iPre = pages-1;

If (pages> = totalpages) iNext = totalpages;
Else iNext = pages + 1;

Int n = (int) (count/pagesize );
If (n * pagesize <count) n ++;
If (n> 1)
{
// For (I = 1; I <= n; I ++) out. print ("<a href = inquire. jsp? Pages =
"+ I +"> "+ I +" </a> ");
// Out. print ("<HR align = center noShade SIZE = 1 width = \" 95% \
"> ");
String lt1 = "back to homepage", lt2 = "first page", lt3 = "Previous Page", lt4 = "next
Page ", lt5 =" last page ", lt6 = "";
Lt6 = "<a href = http://www.zj.hn.cn>" + lt1 + "</
A> "+
Lurlt + "pages =" + iFirst + "> <FONT color = red>" + lt2 + "& nbs
P; </a> "+
Lurlt + "pages =" + iPre + "> <FONT color = red>" + lt3 +"
</A> "+
Lurlt + "pages =" + iNext + "> <FONT color = red>" + lt4 +"
; </A> "+
Lurlt + "pages =" + iLast + "> <FONT color = red>" + lt5 +"
; </A> ";
St = "" +
"<TABLE align = center border = 0 cellPadding = 0 cellSpacing =
0 width = \ "95% \" type = "codeph" text = "codeph"> "+
"<TBODY> <TR> <TD align = middle bgColor = #97 badd width = \" 1
00% \ "> <FONT color = # ff0000>" +
Lt6 +
"</FONT> </TD> </TR> </TBODY> </TABLE> <BR> ";

Out. print (st );

}

}
} Catch (Exception e) {out. println ("error:" + e );}
}

%>

<%
// The End Of The message board interface
String lbottom = "";
Lbottom = lbottom +
"\ N" +
"<FORM action = guestbook. jsp method = post name = sign> \ n" +
"<INPUT name = gbname type = hidden value = cnzjj_gt> \ n" +
"<INPUT name = pages type = hidden value = 1> \ n" +
"<TABLE bgColor = # d5e8fd border = 0 cellSpacing = 1 width = \" 95% \ "> \ n" +

"<TBODY> \ n" +
"<TR> \ n" +
"<TD align = middle bgColor = # e6e6fa colSpan = 2 noWrap> <STRONG> <FONT
Color = blue \ n "+
"Face = _ GB2312 size = 5> For more information, see </FONT> </STRONG> & nbs.
P; [add * content required] </TD> </TR> \ n "+
"<TR> \ n" +
"<TD noWrap width = \" 45% \ "> \ n" +
"<DIV align = left> \ n" +
"<TABLE bgColor = # d5e8fd border = 0 cellSpacing = 1 width = \" 100% \ ">
\ N "+
"<TBODY> \ n" +
"<TR> \ n" +
"<TD noWrap width = \" 100% \ "> * Message Subject: <INPUT maxLength = 40 n
Ame = lw_title \ n "+
"Size = 36> </TD> </TR> \ n" +
"<TR> \ n" +
"<TD noWrap width = \" 100% \ "> * Network Name: <INPUT maxLength = 18 n
Ame = lw_author \ n "+
"Size = 36> </TD> </TR> \ n" +
"<TR> \ n" +
"<TD noWrap width = \" 100% \ "> homepage title: <INPUT maxLength
= 40 name = pagetitle \ n "+
"Size = 36> </TD> </TR> \ n" +
"<TR> \ n" +
"<TD noWrap width = \" 100% \ "> Home address: <INPUT maxLength
= 255 name = author_http \ n "+
"Size = 36> </TD> </TR> \ n" +
"<TR> \ n" +
"<TD noWrap width = \" 100% \ "> * Email: <INPUT maxLength = 40 n
Ame = author_email \ n "+
"Size = 36> </TD> </TR> </TBODY> </TABLE> </DIV> </TD> \ n" +
"<TD noWrap vAlign = top width = \" 55% \ "> \ n" +
"<DIV align = left> \ n" +
"<TABLE bgColor = # b6d7fc border = 0 cellSpacing = 1 width = \" 100% \ ">
\ N "+
"<TBODY> \ n" +
"<TR> \ n" +
"<TD width = \" 100% \ "> enter your message below: </TD> </TR> \ n" +

"<TR> \ n" +
"<TD width = \" 100% \ "> <TEXTAREA cols = 50 name = lw_content rows =
7> </TEXTAREA> </TD> </TR> </TBODY> </TABLE> </DIV> </TD> </TR> \ n "+
"<TR> \ n" +
"<TD bgColor = # fbf7ea colSpan = 2 noWrap> emotion \ n" +
"<INPUT name = gifface type = radio value = 1 checked> <IMG \ n" +
"Alt = \" 1.gif (152 bytes) \ "height = 15 src = \" 1.gif \ "width = 15> <I
NPUT \ n "+
"Name = gifface type = radio value = 2> Height = 15 \ n "+
"Src = \" 2.gif \ "width = 15> <INPUT name = gifface type = radio value = 3
> <IMG \ n "+
"Alt = \" 3.gif (147 bytes) \ "height = 15 src = \" 3.gif \ "width = 15> <I
NPUT \ n "+
"Name = gifface type = radio value = 4> Height = 15 \ n "+
"Src = \" 4.gif \ "width = 15> <INPUT name = gifface type = radio value = 5
> <IMG \ n "+
"Alt = \" 5.gif (118 bytes) \ "height = 15 src = \" 5.gif \ "width = 15> <I
NPUT \ n "+
"Name = gifface type = radio value = 6> Height = 15 \ n "+
"Src = \" 6.gif \ "width = 15> <INPUT name = gifface type = radio value = 7
> <IMG \ n "+
"Alt = \" 7.gif (180 bytes) \ "height = 15 src = \" 7.gif \ "width = 15> <I
NPUT \ n "+
"Name = gifface type = radio value = 8> Height = 15 \ n "+
"Src = \" 8.gif \ "width = 15> <INPUT name = gifface type = radio value = 9
> <IMG \ n "+
"Alt = \" 9.gif (162 bytes) \ "height = 15 src = \" 9.gif \ "width = 15> <I
NPUT \ n "+
"Name = gifface type = radio value = 10> \ "Height = 15 \ n" +
"Src = \" 10.gif \ "width = 15> <INPUT name = gifface type = radio value =
11> <IMG \ n "+
"Alt = \" 11.gif (93 bytes) \ "height = 15 src = \" 11.gif \ "width = 15> <
INPUT \ n "+
"Name = gifface type = radio value = 12> ) \ "Height = 14 \ n" +
"Src = \" 12.gif \ "width = 15> \ n" +
"<INPUT \ n" +
"Name = gifface type = radio value = 13> ) \ "Height = 14 \ n" +
"Src = \" 13.gif \ "width = 15> \ n" +
"<INPUT \ n" +
"Name = gifface type = radio value = 14> ) \ "Height = 14 \ n" +
"Src = \" 14.gif \ "width = 15> \ n" +
"<INPUT \ n" +
"Name = gifface type = radio value = 15> ) \ "Height = 14 \ n" +
"Src = \" 15.gif \ "width = 15> \ n" +
"<INPUT \ n" +
"Name = gifface type = radio value = 16> ) \ "Height = 14 \ n" +
"Src = \" 16.gif \ "width = 15> </TD> \ n" +
"</TR> \ n" +
"<TR> \ n" +
"<TD align = middle colSpan = 2 noWrap> <INPUT name = export go onclick = \" re
Turn ValidInput () \ "type = submit value = \" submit \ "> \ n" +

"<INPUT name = Your Prev onclick = \" return ValidInput () \ "type = submit valu
E = \ "preview \"> \ n "+
"<INPUT name = export cancel type = reset value = \" re-write \ "> <INPU
T name = cmdBack onclick = javascript: history. go (-1) type = button value = \ "back
Back to \ "> \ n" +
"</TD> </TR> </TBODY> </TABLE> </FORM> </CENTER> </DIV> ";
Out. print (lbottom );

%>

</Body>

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.