zl程序教程

您现在的位置是:首页 >  后端

当前栏目

jsp留言板源代码二:给jsp初学者.

JSP 源代码 初学者 留言板
2023-06-13 09:13:45 时间
作者:precom(皮蛋)2000.12.10

guestbook.jsp
===========================
<html><head>
<METAcontent="text/html;charset=gb2312"http-equiv=Content-Type>

<title>张家界电话黄页(网上114)</title></head>
<styletype="text/css">
<!--
BODY{FONT-FAMILY:"宋体","ArialNarrow","TimesNewRoman";FONT-SI
ZE:9pt}
.p1{FONT-FAMILY:"宋体","ArialNarrow","TimesNewRoman";FONT-SIZ
E:12pt}
A:link{COLOR:#00793d;TEXT-DECORATION:none}
A:visited{TEXT-DECORATION:none}
A:hover{TEXT-DECORATION:underline}
TD{FONT-FAMILY:"宋体","ArialNarrow","TimesNewRoman";FONT-SIZE
:9pt}
.p2{FONT-FAMILY:"宋体","ArialNarrow","TimesNewRoman";FONT-SIZ
E:9pt;LINE-HEIGHT:150%}
.p3{FONT-FAMILY:"宋体","ArialNarrow","TimesNewRoman";FONT-SIZ
E:9pt;LINE-HEIGHT:120%}

-->
</style>


<body>


<%@pagecontentType="text/html;charset=GB2312"%>
<%@pagelanguage="java"import="java.sql.*"%>
<jsp:useBeanid="testInq"scope="page"class="ymbean.opDb"/>
<%
intpages=1;
intpagesize=10;
intcount=0;
inttotalpages=0;

Stringcountsql="",inqsql="",lwhere="",insertsql="",st="";

Stringlw_title="",lw_author="",pagetitle="",author_http="",author_e
mail="",lw_ico="",
lw_content="",lw_class1="";
Stringauthor_ip="",lw_time="",lw_class2="",lw_type="",zt_time="",zt
_author="";
intanswer_num=0,click_num=0;
intinquire_item=1;
Stringinquire_itemt="",inquire_value="";
Stringlurlt="<ahref=guestbook.jsp?",llink="";

lwhere="wherelw_type="z"";//只显示主贴

/*
Enumeratione=request.getParameterNames();
while(e.hasMoreElements()){
Stringname=(String)e.nextElement();
*/
try{
//取显示的页页序数
pages=newInteger(request.getParameter("pages")).intValue();
}catch(Exceptione){}
try{
//取查询参数
inquire_item=newInteger(request.getParameter("range")).intValu
e();
inquire_value=newString(request.getParameter("findstr").getByt
es("ISO8859_1"));
if(inquire_item==0)inquire_itemt="lw_title";
elseif(inquire_item==1)inquire_itemt="lw_content";
elseif(inquire_item==2)inquire_itemt="lw_author";
elseif(inquire_item==3)inquire_itemt="lw_time";
elseif(inquire_item==4)inquire_itemt="lw_title";
lwhere=lwhere+"and"+inquire_itemt+"like"%"+inquire_value+"%
"";
lurlt=lurlt+"range="+inquire_item+"&findstr="+inquire_value+"&"


}catch(Exceptione){}

try{
//取得参数留言内容
lw_class1=newString(request.getParameter("gbname").getBytes("ISO885
9_1"));
lw_title=newString(request.getParameter("lw_title").getBytes("ISO88
59_1"));
lw_author=newString(request.getParameter("lw_author").getBytes("ISO
8859_1"));
pagetitle=newString(request.getParameter("pagetitle").getBytes("ISO
8859_1"));
author_http=newString(request.getParameter("author_http").getBytes(
"ISO8859_1"));
author_email=newString(request.getParameter("author_email").getByte
s("ISO8859_1"));
lw_ico=request.getParameter("gifface");
lw_content=newString(request.getParameter("lw_content").getBytes("I
SO8859_1"));
StringrequestMethod=request.getMethod();
requestMethod=requestMethod.toUpperCase();
if(requestMethod.indexOf("POST")<0)
{out.print("非法操作!");
return;
}

//形成其他数据项
author_ip=request.getRemoteAddr();
lw_time=testInq.getCurrentDate("yyyyMMddHHmmss");
lw_class2="2";
lw_type=""+"z";//主贴
zt_time=lw_time;
zt_author=lw_author;
answer_num=0;
click_num=0;
//================
st="","";
//保证留言所有数据项的长度在正常范围内
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(0,4000)


insertsql="insertintoguestbookvalues(""+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);
//插入留言
try{
Stringlmsg=testInq.executeUpdate(insertsql);
if(lmsg.indexOf("executeUpdateok")<0)
out.print("lmsg="+lmsg);

}catch(Exceptione){out.print("错误:"+e);}
}catch(Exceptione){}
%>

<%
//验证留言输入项合法性的javascript
Stringljs="<SCRIPTlanguage=JavaScript>\n"+
"<!--\n"+
"functionValidInput()\n"+
"{if(document.sign.lw_author.value==\"\")\n"+
"{alert(\"请填写您的大名。\");\n"+
"document.sign.lw_author.focus();\n"+
"returnfalse;}\n"+
"if(document.sign.lw_title.value==\"\")\n"+
"{alert(\"请填写留言主题。\");\n"+
"document.sign.lw_title.focus();\n"+
"returnfalse;}\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(\"您填写的EMail无效,请填写一个有效的Email!\");\n"+
"document.sign.author_emaill.focus();\n"+
"returnfalse;\n"+
"}\n"+
"}\n"+
"returntrue;\n"+
"}\n"+
"functionValidSearch()\n"+
"{if(document.frmsearch.findstr.value==\"\")\n"+
"{alert(\"不能搜索空串!\");\n"+
"document.frmsearch.findstr.focus();\n"+
"returnfalse;}\n"+
"}\n"+
"//-->\n"+
"</SCRIPT>";
out.print(ljs);
%>

<%
//留言板界面首部
Stringltop="<DIValign=center>\n"+
"<CENTER>\n"+
"<FORMaction=guestbook.jspmethod=postname=frmsearch>\n"+
"<INPUTname=gbnametype=hiddenvalue=cnzjj_gt>\n"+
"<TABLEalign=centerborder=0cellSpacing=1width=\"95%\">\n"+
"<TBODY>\n"+
"<TR>\n"+
"<TDbgColor=#336699colSpan=2width=\"100%\">\n"+
"<Palign=center><FONTcolor=#ffffffface=楷体_GB2312\n"+
"size=5>欢迎远方的朋友来张家界旅游观光</FONT></P></TD></TR>\n"
+
"<TRbgColor=#6699cc>\n"+
"<TDalign=leftnoWrapwidth=\"50%\">主页:<A\n"+
"href=\"http://www.zj.hn.cn\"target=_blank><FONT\n"+
"color=#ffffff>张家界旅游</FONT></A>管理员:<A\n"
+
"href=\"mailto:dzx@mail.zj.hn.cninfo.net\"><FONTcolor=#ffffff>
一民</FONT></A>\n"+
">><A\n"+
"href=\"http://www.zj.hn.cn\"><FONT\n"+
"color=#ffffff>管理</FONT></A>>><A\n"+
"href=\"http://www.zj.hn.cn\"><FONT\n"+
"color=#ffffff>申请</FONT></A></TD>\n"+
"<TDalign=rightwidth=\"50%\"><SELECTclass=ourfontname=ranges
ize=1>\n"+
"<OPTIONselectedvalue=0>按主题</OPTION><OPTIONvalue=1>按内
容</OPTION>\n"+
"<OPTIONvalue=2>按作者</OPTION><OPTIONvalue=3>按日期</OPTI
ON><OPTION\n"+
"value=4>按主题&内容</OPTION></SELECT><INPUTname=findst
r><INPUTname=searchonclick=\"returnValidSearch()\"type=submitval
ue=\"搜索\">\n"+
"</TD></TR></TBODY></TABLE></FORM>\n"+
"<HRalign=centernoShadeSIZE=1width=\"95%\">\n"+
"</CENTER></div>";
out.print(ltop);
%>

<%
//显示最近时间发表的一页留言
countsql="selectcount(lw_title)fromguestbook"+lwhere;
inqsql="selectlw_title,answer_num,click_num,lw_author,lw_time,expres
sion,"+
"author_email,lw_class1,lw_class2fromguestbook"+lwhere+"o
rderbylw_timedesc";

if(pages>0)
{
try{
try{
ResultSetrcount=testInq.executeQuery(countsql);
if(rcount.next())
{
count=rcount.getInt(1);
}
rcount.close();
}catch(Exceptionel1){out.println("countrecorderror
:"+el1+"<br>");
out.println(countsql);
}

totalpages=(int)(count/pagesize);
if(count>totalpages*pagesize)totalpages++;
st=""+
"<TABLEalign=centerborder=0cellPadding=0cellSpacing=
0width=\"95%\">"+
"<TBODY><TR><TDalign=middlebgColor=#97baddwidth=\"1
00%\"><FONTcolor=#ff0000>"+
"共"+totalpages+"页,"+count+"
条."+"当前页:"+pages+
"</FONT></TD></TR></TBODY></TABLE><BR>";

out.print(st);

//out.print("共"+totalpages+"页,"+
count+"条."+"当前页:"+pages+"<br>");

st="<center>"+
"<TABLEborder=0cellPadding=2cellSpacing=1width=\"95%\">"+
"<TBODY>"+
"<TR>"+
"<TDalign=middlebgColor=#6699ccwidth=\"55%\"><FONT"+
"color=#ffffff>留言主题</FONT></TD>"+
"<TDalign=middlebgColor=#6699ccwidth=50><FONT"+
"color=#ffffff>回应数</FONT></TD>"+
"<TDalign=middlebgColor=#6699ccwidth=40><FONT"+
"color=#ffffff>点击数</FONT></TD>"+
"<TDalign=middlebgColor=#6699ccwidth=100><FONT"+
"color=#ffffff>作者名</FONT></TD>"+
"<TDalign=middlebgColor=#6699ccwidth=140><FONT"+
"color=#ffffff>发表/回应时间</FONT></TD></TR>";
out.print(st);

if(count>0)
{
ResultSetrs=testInq.executeQuery(inqsql);
ResultSetMetaDatametaData=rs.getMetaData();

inti;
//跳过pages-1页,使cursor指向pages并准备显示
for(i=1;i<=(pages-1)*pagesize;i++)rs.next();
//显示第pages页开始
Stringlinestr="";
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="<TRbgColor=#d5e8fd>\n"+
"<TDbgColor=#d5e8fd><IMGsrc=\""+lw_ico+".gif\
"><A"+
"href=\""+llink+"\">"+lw_title+"</A></TD>"+
"<TDalign=middle>["+answer_num+"]</TD>"+
"<TDalign=middle>"+click_num+"</TD>"+
"<TDalign=middle><Ahref=\"mailto:"+author_ema
il+"\">"+lw_author+"</A></TD>"+
"<TDalign=middle>"+st+"</TD></TR>";

out.println(linestr);

}
rs.close();
//显示第pages页结束
st="</TBODY></TABLE><BR>";
out.print(st);

intiFirst=1,iLast=totalpages,iPre,iNext;
if(pages<=1)iPre=1;
elseiPre=pages-1;

if(pages>=totalpages)iNext=totalpages;
elseiNext=pages+1;

intn=(int)(count/pagesize);
if(n*pagesize<count)n++;
if(n>1)
{
//for(i=1;i<=n;i++)out.print("<ahref=inquire.jsp?pages=
"+i+">"+i+"</a>");
//out.print("<HRalign=centernoShadeSIZE=1width=\"95%\
">");
Stringlt1="返回主页",lt2="第一页",lt3="上一页",lt4="下一
页",lt5="最后一页",lt6="";
lt6="<ahref=http://www.zj.hn.cn>"+lt1+"</
a>"+
lurlt+"pages="+iFirst+"><FONTcolor=red>"+lt2+"&nbs
p;</a>"+
lurlt+"pages="+iPre+"><FONTcolor=red>"+lt3+"
</a>"+
lurlt+"pages="+iNext+"><FONTcolor=red>"+lt4+"
;</a>"+
lurlt+"pages="+iLast+"><FONTcolor=red>"+lt5+"
;</a>";
st=""+
"<TABLEalign=centerborder=0cellPadding=0cellSpacing=
0width=\"95%\">"+
"<TBODY><TR><TDalign=middlebgColor=#97baddwidth=\"1
00%\"><FONTcolor=#ff0000>"+
lt6+
"</FONT></TD></TR></TBODY></TABLE><BR>";

out.print(st);

}

}
}catch(Exceptione){out.println("error:"+e);}
}


%>


<%
//留言板界面尾部
Stringlbottom="";
lbottom=lbottom+
"\n"+
"<FORMaction=guestbook.jspmethod=postname=sign>\n"+
"<INPUTname=gbnametype=hiddenvalue=cnzjj_gt>\n"+
"<INPUTname=pagestype=hiddenvalue=1>\n"+
"<TABLEbgColor=#d5e8fdborder=0cellSpacing=1width=\"95%\">\n"+

"<TBODY>\n"+
"<TR>\n"+
"<TDalign=middlebgColor=#e6e6facolSpan=2noWrap><STRONG><FONT
color=blue\n"+
"face=楷体_GB2312size=5>发表意见</FONT></STRONG>&nbs
p;[加*的内容必须填写]</TD></TR>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"45%\">\n"+
"<DIValign=left>\n"+
"<TABLEbgColor=#d5e8fdborder=0cellSpacing=1width=\"100%\">
\n"+
"<TBODY>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"100%\">*留言主题:<INPUTmaxLength=40n
ame=lw_title\n"+
"size=36></TD></TR>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"100%\">*网上大名:<INPUTmaxLength=18n
ame=lw_author\n"+
"size=36></TD></TR>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"100%\">主页标题:<INPUTmaxLength
=40name=pagetitle\n"+
"size=36></TD></TR>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"100%\">主页地址:<INPUTmaxLength
=255name=author_http\n"+
"size=36></TD></TR>\n"+
"<TR>\n"+
"<TDnoWrapwidth=\"100%\">*电子邮件:<INPUTmaxLength=40n
ame=author_email\n"+
"size=36></TD></TR></TBODY></TABLE></DIV></TD>\n"+
"<TDnoWrapvAlign=topwidth=\"55%\">\n"+
"<DIValign=left>\n"+
"<TABLEbgColor=#b6d7fcborder=0cellSpacing=1width=\"100%\">
\n"+
"<TBODY>\n"+
"<TR>\n"+
"<TDwidth=\"100%\">请在下面填写你的留言:</TD></TR>\n"+

"<TR>\n"+
"<TDwidth=\"100%\"><TEXTAREAcols=50name=lw_contentrows=
7></TEXTAREA></TD></TR></TBODY></TABLE></DIV></TD></TR>\n"+
"<TR>\n"+
"<TDbgColor=#fbf7eacolSpan=2noWrap>表情\n"+
"<INPUTname=giffacetype=radiovalue=1checked><IMG\n"+
"alt=\"1.gif(152bytes)\"height=15src=\"1.gif\"width=15><I
NPUT\n"+
"name=giffacetype=radiovalue=2><IMGalt=\"2.gif(174bytes)\"
height=15\n"+
"src=\"2.gif\"width=15><INPUTname=giffacetype=radiovalue=3
><IMG\n"+
"alt=\"3.gif(147bytes)\"height=15src=\"3.gif\"width=15><I
NPUT\n"+
"name=giffacetype=radiovalue=4><IMGalt=\"4.gif(172bytes)\"
height=15\n"+
"src=\"4.gif\"width=15><INPUTname=giffacetype=radiovalue=5
><IMG\n"+
"alt=\"5.gif(118bytes)\"height=15src=\"5.gif\"width=15><I
NPUT\n"+
"name=giffacetype=radiovalue=6><IMGalt=\"6.gif(180bytes)\"
height=15\n"+
"src=\"6.gif\"width=15><INPUTname=giffacetype=radiovalue=7
><IMG\n"+
"alt=\"7.gif(180bytes)\"height=15src=\"7.gif\"width=15><I
NPUT\n"+
"name=giffacetype=radiovalue=8><IMGalt=\"8.gif(96bytes)\"
height=15\n"+
"src=\"8.gif\"width=15><INPUTname=giffacetype=radiovalue=9
><IMG\n"+
"alt=\"9.gif(162bytes)\"height=15src=\"9.gif\"width=15><I
NPUT\n"+
"name=giffacetype=radiovalue=10><IMGalt=\"10.gif(113bytes)
\"height=15\n"+
"src=\"10.gif\"width=15><INPUTname=giffacetype=radiovalue=
11><IMG\n"+
"alt=\"11.gif(93bytes)\"height=15src=\"11.gif\"width=15><
INPUT\n"+
"name=giffacetype=radiovalue=12><IMGalt=\"12.gif(149bytes
)\"height=14\n"+
"src=\"12.gif\"width=15>\n"+
"<INPUT\n"+
"name=giffacetype=radiovalue=13><IMGalt=\"13.gif(149bytes
)\"height=14\n"+
"src=\"13.gif\"width=15>\n"+
"<INPUT\n"+
"name=giffacetype=radiovalue=14><IMGalt=\"14.gif(149bytes
)\"height=14\n"+
"src=\"14.gif\"width=15>\n"+
"<INPUT\n"+
"name=giffacetype=radiovalue=15><IMGalt=\"15.gif(149bytes
)\"height=14\n"+
"src=\"15.gif\"width=15>\n"+
"<INPUT\n"+
"name=giffacetype=radiovalue=16><IMGalt=\"16.gif(149bytes
)\"height=14\n"+
"src=\"16.gif\"width=15></TD>\n"+
"</TR>\n"+
"<TR>\n"+
"<TDalign=middlecolSpan=2noWrap><INPUTname=cmdGOonclick=\"re
turnValidInput()\"type=submitvalue=\"提交\">\n"+

"<INPUTname=cmdPrevonclick=\"returnValidInput()\"type=submitvalu
e=\"预览\">\n"+
"<INPUTname=cmdCanceltype=resetvalue=\"重写\"><INPU
Tname=cmdBackonclick=javascript:history.go(-1)type=buttonvalue=\"返
回\">\n"+
"</TD></TR></TBODY></TABLE></FORM></CENTER></DIV>";
out.print(lbottom);

%>

</body></html>