提交的表单用到的JavaScript代码:
<script language="javascript" >
function check()
{
if(document.getElementById("name1").value=="null")
{
alert("你现在的用户名为:"+document.getElementById("name1").value+"请先登录,再留言!");
return false;
}else{
alert("恭喜您:"+document.getElementById("name1").value+" 留言发表成功!");
return true;
}
}
</script>
发表留言的提交表单:
<!--注意onSubmit的使用,如果JS函数check( )返回的是true,则执行表单的提交;如果返回的是false,就不执行表单的提交-->
<form name="form1" method="post" action="UserManager" οnsubmit="return check();">
<input type="hidden" name="action" value="ly" />
<input type="hidden" name="name1" id="name1" value="<%=(String)request.getSession().getAttribute("user_name")%>" />
标题:<input type="text" name="title" value="" size="50"/>
<textarea style="border-color:#6699FF" name="content" cols="80" rows="4"></textarea><br/>
<input type="submit" value="发表" align="right" />
</form>
servlet中的处理代码:
if("ly".equals(action))
{
String t=LYservice.nowTime();//在LYservice类中调用静态方法nowTime()获取和格式化系统时间
LY ly=new LY();//LY为留言的实体类
ly.setTitle(request.getParameter("title"));
ly.setContent(request.getParameter("content"));
ly.setLy_time(t);//留言的时间
ly.setLy_name((String) request.getSession().getAttribute("user_name"));//获取session中的留言者姓名
LYservice lys=new LYservice();
try {
if(lys.FaBiao(ly))
{
request.getRequestDispatcher("customer/ly_1.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
留言相关的业务处理代码:
public class LYservice
{
public List list_LY()
{
java.sql.Connection conn=DB.getConnection();
ResultSet rs = null;
try {
String sql = "select * from market_ly";
Statement sm=conn.createStatement();
rs = sm.executeQuery(sql);
LY y = null;
Vector vector = new Vector();
// ResultSet指针最初位于第一行之前,调用next方法使下一行成为当前行
while (rs.next()) {
y = new LY();
y.setId(rs.getInt("id"));
y.setTitle(rs.getString("title"));
y.setContent(rs.getString("content"));
y.setLy_name(rs.getString("ly_name"));
y.setLy_time(rs.getString("ly_time"));
vector.addElement(y);
}
return vector;
} catch (SQLException SqlE) {
SqlE.printStackTrace();
return null;
} catch (Exception E) {
E.printStackTrace();
return null;
}
finally {
// 关闭连接,释放数据库资源:
DB.close(conn);
}
}
//获取并格式化当前系统时间
public static String nowTime()
{
Calendar c = Calendar.getInstance();
c.setTimeInMillis(new Date().getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(c.getTime());
}
//发表留言的方法
public Boolean FaBiao(LY ly) throws SQLException
{
Boolean flag=false;
Connection con = null;
PreparedStatement pstmt = null;
//Statement stmt = null;
con = DB.getConnection();
//stmt=con.createStatement();
String sql = "insert into market_ly(title,content,ly_name,ly_time) values(?,?,?,?)";
pstmt = DB.prepare(con, sql);
String title=null;
String content=null;
String ly_name=null;
try {
title=new String(ly.getTitle().getBytes("utf-8"), "utf-8");
//System.out.println("(转换前)测试1------------"+ly.getContent().getBytes("utf-8"));
content=new String(ly.getContent().getBytes("utf-8"), "utf-8");
//System.out.println("(转换为uft-8)测试2------------"+content);
ly_name=new String(ly.getLy_name().getBytes("utf-8"), "utf-8");
} catch (Exception e) {
e.printStackTrace();}
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setString(3, ly_name);
pstmt.setString(4, ly.getLy_time());
//String sqltest="insert into market_ly(title,content,ly_name,ly_time) values(\'"
//+title+"\',\'"+content+"\',\'"+ly_name+"\',\'"+ly.getLy_time()+"\');";
System.out.println(sql);
int a = pstmt.executeUpdate();
//int a=stmt.executeUpdate(sqltest);
if(a==1)
{
flag=true;
System.out.println("发布留言数据成功!");
}
DB.close(pstmt);
DB.close(con);
return flag;
}
}
jsp页面中的留言显示代码:
<%
LYservice es=new LYservice();
List<LY> lys=es.list_LY();
for(LY y:lys)
{
%>
<tr>
<td bgcolor="#FFCCFF" ><span class="STYLE8"><%=y.getId() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getTitle() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getLy_time() %></span></td>
<td bgcolor="#FFCCFF"><span class="STYLE8"><%=y.getContent() %></span></td>
</tr>
<br/>
<%
}
%>