当前位置:编程学习 > JSP >>

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

答案:作 者: precom (皮蛋) 2000.12.10

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=>
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> \n"+
" <!-- \n"+
" function ValidInput() \n"+
" {if(document.sign.lw_author.value==\"\") \n"+
" {alert(\"请填写您的大名。\"); \n"+
" document.sign.lw_author.focus(); \n"+
" return false;} \n"+
" if(document.sign.lw_title.value==\"\") \n"+
" {alert(\"请填写留言主题。\"); \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(\"您填写的EMail无效,请填写一个有效的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(\"不能搜索空串!\"); \n"+
" document.frmsearch.findstr.focus(); \n"+
" return false;} \n"+
" } \n"+
" //--> \n"+
" </SCRIPT> ";
out.print(ljs);
%>

<%
//留言板界面首部
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>欢迎远方的朋友来张家界旅游观光</FONT></P></TD></

上一个:我认为JSP有问题(下)
下一个:初学jsp心得

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,