交到的operate_u.jsp源代码如下。
operate_u.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java. sql.*" errorPage="" %>
<%@page import="java.util.Date"%>
<%@page import="java.util.*"%>
<%@page import="java.text.*"%>
<%@ include file="conn.jsp" %>
<%
String username=new String(request.getParameter("username").getBytes("iso-8859-1"));
String userpwd=new String(request.getParameter("userpwd").getBytes("iso-8859-1"));
String name=new String(request.getParameter("name").getBytes("iso-8859-1"));
String qq = new String(request.getParameter("qq").getBytes("iso-8859-1"));
String email = new String(request.getParameter("email").getBytes("iso-8859-1"));
String http = new String(request.getParameter("http").getBytes("iso-8859-1"));
String sex=new String(request.getParameter("sex").getBytes("iso-8859-1"));
Date currTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String ftime=new String(formatter.format(currTime).getBytes("iso-8859-1"));
String condition="insert into guestbook_user (username,userpwd,name,qq,email, http,regtime,sex) values('"+username+"','"+userpwd+"','"+name+"','"+qq+"','"+email+"',
'"+http+"','"+ftime+"','"+sex+"')";
stmt.executeUpdate(condition);
stmt.close();
conn.close();
response.sendRedirect("index.jsp");
%>
这段程序就是接收上一个页面表单提交过来的值,对应到相应的字段并写入guestbook_user表中,这里有一点要注意的是,对于有可能出现中文的值在写入数据库之前必须要先进行编码转换,即如:Stringusername=newString(request.getParameter("username").getBytes("iso-8859-1"));就是以iso-8859-1编码的方式接收表单信息,并转换为页面默认编码(这里是gb2312),这样就可以避免出现中文乱码问题。
下面来介绍用户添加留言页面中的主要部分。
add.jsp
<%if (null == session.getAttribute("userid")){%>
<tr valign="top">
<td colspan="2" align="center"> <form name="form1" method="post" action="?act=login">
<table width="60%" cellspacing="0" cellpadding="0">
<tr>
<td height="25" align="right" class="font0">用户名:</td>
<td><input name="username" type="text" id="username" style="border: 1px double rgb(187,185,185);font:9pt"></td>
</tr>
<tr>
<td height="25" align="right" class="font0">密码:</td>
<td><input name="userpwd" type="password" id="userpwd" style= "border:1px double rgb(187,185,185);font:9pt"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit3" value="登录" style="border: 1 solid #BBB9B9;background:#ffffff" class="button"></td>
</tr>
</table>
</form></td>
</tr>
<form action="operate_m.jsp" method="POST" name="form" id="form" onsubmit= "return check()">
<tr valign="top">
<td height="162" colspan="2" valign="top"> <table width="100%" border="0" cellpadding="0" cellspacing="2" class="tittle1">
<tr>
<td width="70" height="21" align="right"><font color="#000099"><img src="img/Forum_nav.gif" width="15" height="19" align="absmiddle">
<span class="font0">姓名:</span></font></td>
<td width="180" height="30"> <input name="name" type="text" id="name" style="border:1px double rgb(187,185,185);font:9pt">
<font color="#FF0000">*</font> </td>
<td rowspan="4" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><font color="#000099"><img src="img/shareforum.gif" width="25" height="25" align="absmiddle">
<span class="font0">留言内容:</span></font></td>
</tr>
<tr>
<td align="center"> <textarea name="content" cols="55" rows="5" id="content" style="border:1px double rgb(187,185,185);font:9pt"></textarea>
<font color="#FF0000">*</font> </td>
</tr>
</table></td>
</tr>
<tr>
<td align="right"><font color="#000099"><img src="img/topicnew. gif" width="16" height="18" align="absmiddle"><span class="font0">
性别:</span></font></td>
<td height="30"> <span class="font0">男孩</span> <input type="radio" name="sex" value="0" checked>
<span class="font0">女孩</span> <input type="radio" name="sex" value="1">
</td>
</tr>
<tr>
<td height="21" align="right"><font color="#000099"><img src="img/ topichot2.gif" width="16" height="18" align="absmiddle"><span class="font0">
OICQ:</span></font></td>
<td height="30"> <input name="qq" type="text" id="qq" style= "border:1px double rgb(187,185,185);font:9pt">
</td>
</tr>
<tr>
<td height="21" align="right"><font color="#000099"><img src="img/ topichotnonew.gif" width="16" height="18" align="absmiddle"><span class="font0"> Email:</span></font></td>
<td height="30"> <input name="email" type="text" id="email" style="border:1px double rgb(187,185,185);font:9pt">
</td>
</tr>
<tr>
<td height="21" align="right"><font color="#000099"><img src="img/ topicnonew.gif" width="16" height="18" align="absmiddle">
<span class="font0">主页:</span></font></td>
<td height="30"> <input name="http" type="text" id="http" style="border:1px double rgb(187,185,185);font:9pt" value="http://">
</td>
<td align="center"> <input type="submit" name="Submit" value="提交留言" style="border:1 solid #BBB9B9;background:#ffffff" class="button">
<input type="reset" name="Submit2" value="取消重写" style="border: 1 solid #BBB9B9;background:#ffffff" class="button">
</td>
</tr>
</table></td>
</tr>
</form>
<%}else{%>
<form action="operate_m.jsp" method="POST" name="form2" id="form2" onsubmit= "return check()">
<tr>
<td width="50"> </td>
<td><font color="#000099"><img src="img/shareforum.gif" width="25" height= "25" align="absmiddle">
<span class="font0">留言内容:</span></font></td>
</tr>
<tr>
<td> </td>
<td><textarea name="content" cols="55" rows="5" id="content" style= "border:1px double rgb(187,185,185);font:9pt"></textarea>
<font color="#FF0000">*</font> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit4" value="提交留言" style="border:1 solid #BBB9B9;background:#ffffff" class="button">
<input type="reset" name="Submit22" value="取消重写" style="border:1 solid #BBB9B9;background:#ffffff" class="button">
</td>
</tr>
</form>
<%}%>
这段程序中包含了注册用户留言和非注册用户留言两个部分,系统根据session.getAttribute ("userid")的值是否为空来判断是否为已注册并登录用户留言。