jsp留言板实例源代码

这个博客展示了如何使用JSP创建一个简单的留言板系统。它包括Oracle数据库中guestbook表的创建,以及guestbook.jsp页面的HTML和Java代码,用于用户输入留言、搜索和显示已有的留言。页面还包含了一些样式和JavaScript验证功能,确保用户输入的有效性。
摘要由CSDN通过智能技术生成
 
oracle数据表创建.

create table guestbook (
lw_title varchar2(100) not null, --留言主题
lw_author varchar2(20) not null, --网上大名 pk
author_http varchar2(40) , --主页地址
author_email varchar2(40) not null,--电子信箱 pk
expression varchar2(2) not null,--表情
lw_time varchar2(14) not null,--发表回应时间
answer_num number(3) not null,--回应数
click_num number(4) not null,--点击数
author_ip varchar2(16) ,--来源ip
lw_class1 varchar2(10) not null,--留言板大类 pk
lw_class2 varchar2(20) not null,--论坛栏目 pk
lw_type varchar2(1) not null,--留言类别,即是否为主贴

zt_time varchar2(14) ,--主贴时间
zt_author varchar2(40) ,--主贴email
lw_content varchar2(4000) ,--内容
constraint pk_guestbook primary key(lw_class1,lw_class2,lw
_time,lw_author)
) storage (initial 5M next 5M pctincrease 1);  <script language=javascript src="/js/contentad.js" type=text/javascript></script>
guestbook.jsp
===========================
<html><head>
<META content="text/html; charset=gb2312 " http-equiv=Content-Type>

<title>张家界电话黄页(网上114)</title></head>
<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 "; //只显示主贴

/*
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
*/
try{
//取显示的页页序数
pages = new Integer(request.getParameter("pages")).intValue();
} catch (Exception e) {}
try{
//取查询参数
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{
//取得参数 留言内容
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("非法操作!");
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="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);
//插入留言
try{
String lmsg=testInq.executeUpdate(insertsql);
if(lmsg.indexOf("executeUpdate ok")<0)
out.print("lmsg="+lmsg);

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

<%
//验证留言输入项合法性的javascript
String ljs=" <SCRIPT language=JavaScript>
"+
" <!--
"+
" function ValidInput()
"+
" {if(document.sign.lw_author.value=="")
"+
" {alert("请填写您的大名。");
"+
" document.sign.lw_author.focus();
"+
" return false;}
"+
" if(document.sign.lw_title.value=="")
"+
" {alert("请填写留言主题。");
"+
" document.sign.lw_title.focus();
"+
" return false;}
"+
" if (document.sign.author_email.value!="")
"+
" { if ((document.sign.author_email.value.indexOf("@")<0)//(document
.sign.author_email.value.indexOf(":")!=-1))
"+
" {alert("您填写的EMail无效,请填写一个有效的Email!");
"+
" document.sign.author_emaill.focus();
"+
" return false;
"+
" }
"+
" }
"+
" return true;
"+
" }
"+
" function ValidSearch()
"+
" { if(document.frmsearch.findstr.value=="")
"+
" {alert("不能搜索空串!");
"+
" document.frmsearch.findstr.focus();
"+
" return false;}
"+
" }
"+
" //-->
"+
" </SCRIPT> ";
out.print(ljs);
%>

<%
//留言板界面首部
String ltop=" <DIV align=center>
"+
" <CENTER>
"+
" <FORM action=guestbook.jsp method=post name=frmsearch>
"+
" <INPUT name=gbname type=hidden value=cnzjj_gt>
"+
" <TABLE align=center border=0 cellSpacing=1 width="95%">
"+
" <TBODY>
"+
" <TR>
"+
" <TD bgColor=#336699 colSpan=2 width="100%">
"+
" <P align=center><FONT color=#ffffff face=楷体_GB2312
"+
" size=5>欢迎远方的朋友来张家界旅游观光</FONT></P></TD></TR>
"
+
" <TR bgColor=#6699cc>
"+
" <TD align=left noWrap width="50%">主页: <A
"+
" href="http://www.zj.hn.cn" target=_blank><FONT
"+
" color=#ffffff>张家界旅游</FONT></A> 管理员: <A
"
+
" href="mailto:dzx@mail.zj.hn.cninfo.net"><FONT color=#ffffff>
一民</FONT></A>
"+
" >><A
"+
" href="http://www.zj.hn.cn "><FONT
"+
" color=#ffffff>管理</FONT></A> >><A
"+
" href=" http://www.zj.hn.cn "><FONT
"+
" color=#ffffff>申请</FONT></A> </TD>
"+
" <TD align=right width="50%"><SELECT class=ourfont name=range s
ize=1>
"+
" <OPTION selected value=0>按主题</OPTION> <OPTION value=1>按内
容</OPTION>
"+
" <OPTION value=2>按作者</OPTION> <OPTION value=3>按日期</OPTI
ON> <OPTION
"+
" value=4>按主题&内容</OPTION></SELECT> <INPUT name=findst
r> <INPUT name=search οnclick="return ValidSearch()" type=submit val
ue="搜 索//">
"+
" </TD></TR></TBODY></TABLE></FORM>
"+
" <HR align=center noShade SIZE=1 width="95%">
"+
" </CENTER></div> ";
out.print(ltop);
%>

<%
//显示最近时间发表的一页留言
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.executeQuery(countsql);
if(rcount.next())
{
count = rcount.getInt(1);
}
rcount.close();
} catch (Exception el1) { out.println("count record error
: "+el1+"
" );
out.println(co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值