(MessagesBean)
package com.lyh.loginDao;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import com.lyh.loginDao.MessageBO;
import com.mysql.jdbc.Connection;
public class MessageBean{
private Connection con;
MessageBO msg;
public MessageBean(){
String CLASSFORNAME="com.mysql.jdbc.Driver";
String SERVANDDB="jdbc:mysql://localhost:3306/forum?useUnicode=true&characterEncoding=UTF-8 ";
String USER="root";
String PWD="123123";
try {
Class.forName(CLASSFORNAME);
con=(Connection) DriverManager.getConnection(SERVANDDB, USER, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setMessage(MessageBO msg){
this.msg=msg;
}
public void addMessage()throws Exception{
//response.setContentType("text/html; charset=utf-8");
try {
PreparedStatement stm= con.prepareStatement("insert into text1 values(?,?,?,?)");
stm.setString(1, msg.getTitle());
stm.setString(2, msg.getName());
System.out.println(msg.getName());
if(msg.getEmail().length()==0){
stm.setString(3, null);
}else{
stm.setString(3, msg.getEmail());
}
stm.setString(4, msg.getContent());
stm.setDate(5, new java.sql.Date(new java.util.Date().getTime()));
System.out.println(new java.util.Date().getTime());
stm.executeUpdate();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Collection<MessageBO> getMessage()throws Exception{
Collection<MessageBO> ret=new ArrayList<MessageBO>();
try {
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from text1");
int message_count=0;
if(result.next()){
message_count=result.getInt(1);
result.close();
}
if(message_count>0){
result=stm.executeQuery("select * from text1 order by date desc");
while(result.next()){
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("email");
String content=result.getString("content");
java.sql.Date date=result.getDate("date");
MessageBO message=new MessageBO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setEmail(mail);
message.setDate(date);
ret.add(message);
}
result.close();
stm.close();
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
(MessagesBO)
package com.lyh.loginDao;
import java.io.Serializable;
import java.sql.Date;
public class MessageBO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name,email,title,content;
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
(addMessage.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html; charset=UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.sql.*,com.lyh.loginDao.*,java.util.*,java.sql.*"%>
<jsp:useBean id="messageBO" class="com.lyh.loginDao.MessageBean" scope="page">
</jsp:useBean>
<jsp:setProperty name="messageBO" property="*" />
<jsp:useBean id="messageBean" class="com.lyh.loginDao.MessageBean" scope="page">
</jsp:useBean>
<%try{
messageBean.getMessage();
messageBean.addMessage();
}catch(Exception e){
e.printStackTrace();
}%>
<html>
<head>
<base href="<%=basePath%>">
<title>add message into table</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<jsp:forward page="messageshow.jsp"/></body>
</body>
</html>
(messageshow.jsp)
<%@page import="com.lyh.loginDao.MessageBO"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html; charset=UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.sql.*, com.lyh.loginDao.*,java.util.*,java.sql.*"%>
<jsp:useBean id="messageBean" class="com.lyh.loginDao.MessageBean"
scope="page">
</jsp:useBean>
<jsp:setProperty property="*" name="messageBean"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>show the message in the table</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<p align="center">
所有访客留言
</p>
<hr>
<%
int message_count=0;
Collection<MessageBO> messages=messageBean.getMessage();
Iterator<MessageBO> it= messages.iterator();
while(it.hasNext()){
MessageBO message=(MessageBO)it.next();
%>
<table width="100%" align="center" border="1" cellpadding="0"
cellspacing="0" bordercolor="99ccff">
<tr>
<td bgcolor="#CCCC99">
<font size="2">主题:</font>
</td>
<td colspan="3"><%=message.getTitle() %></td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size="2">留言人:</font>
</td>
<td><%=message.getName() %></td>
<td bgcolor="#CCCC99">
<font size="2">Email:</font>
</td>
<td>
<%out.println("<a href=mailto:"+message.getEmail()+">"+message.getEmail()+"</a>"); %>
</td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size="2"> 留言时间:</font>
</td>
<td colspan="3">
<%out.println("<font size=2"+message.getDate().toLocaleString()+"</font>"); %>
</td>
</tr>
<tr>
<td align="center">
<%out.println("("+message_count+")"); %>
</td>
<td colspan="3"><%=message.getContent() %></td>
</tr>
</table>
<%
out.println("</hr>");
message_count++;
} %>
<p align="center">
<a href="/ch01/shouwMessage.jsp">我要留言</a>
</p>
</body>
</html>
(shouwMessages.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page contentType="text/html; charset=utf-8"%>
<%
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>message board</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table align="center" border="1" cellpadding="0" cellspacing="0"
bordercolor="#CCCC99">
<tr>
<td align="center" bgcolor="#CCCC99">留言板</td>
</tr>
<tr>
<td>
<form method="post" action="ch01/addMessage.jsp">
<table>
<tr>
<td>姓名:</td>
<td bgcolor="#CCCC99"><input name="name" type="text"
size="25"></td>
</tr>
<tr>
<td bgcolor="#CCCC99">E-mail:</td>
<td bgcolor="#CCCC99"><input name="email" type="text"
size="25"></td>
</tr>
<tr>
<td>主题:</td>
<td bgcolor="#CCCC99"><input type="text" name="title"
size="25"></td>
</tr>
<tr>
<td valign="top" bgcolor="#CCCC99">留言:</td>
<td><textarea rows="7" cols="25" name="content"></textarea>
</td>
</tr>
<tr>
<td colspan="3">
<table align="center" width="100%" cellpadding="0"
cellspacing="0">
<tr>
<td align="center" bgcolor="#CCCC99"><input type="submit"
name="messageBO" value="提交留言"></td>
<td align="center"><a href="ch01/messageshow.jsp"><font
size="2">查看留言</font> </a></td>
<td align="center" bgcolor="#CCCC99"><input type="reset"
value="重新填写"></td>
</tr>
</table>
</td>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
添加html代码呀,没有高亮很难看的
package com.lyh.loginDao; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import com.lyh.loginDao.MessageBO; import com.mysql.jdbc.Connection; public class MessageBean{ private Connection con; MessageBO msg; public MessageBean(){ String CLASSFORNAME="com.mysql.jdbc.Driver"; String SERVANDDB="jdbc:mysql://localhost:3306/forum?useUnicode=true&characterEncoding=UTF-8 "; String USER="root"; String PWD="123123"; try { Class.forName(CLASSFORNAME); con=(Connection) DriverManager.getConnection(SERVANDDB, USER, PWD); } catch (Exception e) { e.printStackTrace(); } } public void setMessage(MessageBO msg){ this.msg=msg; } public void addMessage()throws Exception{ //response.setContentType("text/html; charset=utf-8"); try { PreparedStatement stm= con.prepareStatement("insert into text1 values(?,?,?,?)"); stm.setString(1, msg.getTitle()); stm.setString(2, msg.getName()); System.out.println(msg.getName()); if(msg.getEmail().length()==0){ stm.setString(3, null); }else{ stm.setString(3, msg.getEmail()); } stm.setString(4, msg.getContent()); stm.setDate(5, new java.sql.Date(new java.util.Date().getTime())); System.out.println(new java.util.Date().getTime()); stm.executeUpdate(); con.close(); } catch (Exception e) { e.printStackTrace(); } } public Collection<MessageBO> getMessage()throws Exception{ Collection<MessageBO> ret=new ArrayList<MessageBO>(); try { Statement stm=con.createStatement(); ResultSet result=stm.executeQuery("select count(*) from text1"); int message_count=0; if(result.next()){ message_count=result.getInt(1); result.close(); } if(message_count>0){ result=stm.executeQuery("select * from text1 order by date desc"); while(result.next()){ String title=result.getString("title"); String name=result.getString("name"); String mail=result.getString("email"); String content=result.getString("content"); java.sql.Date date=result.getDate("date"); MessageBO message=new MessageBO(); message.setName(name); message.setTitle(title); message.setContent(content); message.setEmail(mail); message.setDate(date); ret.add(message); } result.close(); stm.close(); } con.close(); } catch (Exception e) { e.printStackTrace(); } return ret; } }
package com.lyh.loginDao; import java.io.Serializable; import java.sql.Date; public class MessageBO implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String name,email,title,content; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page contentType="text/html; charset=UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@page import="java.sql.*,com.lyh.loginDao.*,java.util.*,java.sql.*"%> <jsp:useBean id="messageBO" class="com.lyh.loginDao.MessageBean" scope="page"> </jsp:useBean> <jsp:setProperty name="messageBO" property="*" /> <jsp:useBean id="messageBean" class="com.lyh.loginDao.MessageBean" scope="page"> </jsp:useBean> <%try{ messageBean.getMessage(); messageBean.addMessage(); }catch(Exception e){ e.printStackTrace(); }%> <html> <head> <base href="<%=basePath%>"> <title>add message into table</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <jsp:forward page="messageshow.jsp"/></body> </body> </html>
<%@page import="com.lyh.loginDao.MessageBO"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page contentType="text/html; charset=UTF-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@page import="java.sql.*, com.lyh.loginDao.*,java.util.*,java.sql.*"%> <jsp:useBean id="messageBean" class="com.lyh.loginDao.MessageBean" scope="page"> </jsp:useBean> <jsp:setProperty property="*" name="messageBean"/> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>show the message in the table</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <p align="center"> 所有访客留言 </p> <hr> <% int message_count=0; Collection<MessageBO> messages=messageBean.getMessage(); Iterator<MessageBO> it= messages.iterator(); while(it.hasNext()){ MessageBO message=(MessageBO)it.next(); %> <table width="100%" align="center" border="1" cellpadding="0" cellspacing="0" bordercolor="99ccff"> <tr> <td bgcolor="#CCCC99"> <font size="2">主题:</font> </td> <td colspan="3"><%=message.getTitle() %></td> </tr> <tr> <td bgcolor="#CCCC99"> <font size="2">留言人:</font> </td> <td><%=message.getName() %></td> <td bgcolor="#CCCC99"> <font size="2">Email:</font> </td> <td> <%out.println("<a href=mailto:"+message.getEmail()+">"+message.getEmail()+"</a>"); %> </td> </tr> <tr> <td bgcolor="#CCCC99"> <font size="2"> 留言时间:</font> </td> <td colspan="3"> <%out.println("<font size=2"+message.getDate().toLocaleString()+"</font>"); %> </td> </tr> <tr> <td align="center"> <%out.println("("+message_count+")"); %> </td> <td colspan="3"><%=message.getContent() %></td> </tr> </table> <% out.println("</hr>"); message_count++; } %> <p align="center"> <a href="/ch01/shouwMessage.jsp">我要留言</a> </p> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page contentType="text/html; charset=utf-8"%> <% response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>message board</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <table align="center" border="1" cellpadding="0" cellspacing="0" bordercolor="#CCCC99"> <tr> <td align="center" bgcolor="#CCCC99">留言板</td> </tr> <tr> <td> <form method="post" action="ch01/addMessage.jsp"> <table> <tr> <td>姓名:</td> <td bgcolor="#CCCC99"><input name="name" type="text" size="25"></td> </tr> <tr> <td bgcolor="#CCCC99">E-mail:</td> <td bgcolor="#CCCC99"><input name="email" type="text" size="25"></td> </tr> <tr> <td>主题:</td> <td bgcolor="#CCCC99"><input type="text" name="title" size="25"></td> </tr> <tr> <td valign="top" bgcolor="#CCCC99">留言:</td> <td><textarea rows="7" cols="25" name="content"></textarea> </td> </tr> <tr> <td colspan="3"> <table align="center" width="100%" cellpadding="0" cellspacing="0"> <tr> <td align="center" bgcolor="#CCCC99"><input type="submit" name="messageBO" value="提交留言"></td> <td align="center"><a href="ch01/messageshow.jsp"><font size="2">查看留言</font> </a></td> <td align="center" bgcolor="#CCCC99"><input type="reset" value="重新填写"></td> </tr> </table> </td> </table> </form> </td> </tr> </table> </body> </html>
首先,格式化下楼主代码。回答我放在评论里。
@蔡永吉: 我才刚学java web 不会用AJAX 我具体是哪错了 能帮我修改下错误的地方吗 谢谢
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page contentType="text/html; charset=UTF-8" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><%@page import="java.sql.*,com.lyh.loginDao.*,java.util.*,java.sql.*"%><jsp:useBean id="messageBO" class="com.lyh.loginDao.MessageBean" scope="page"></jsp:useBean><jsp:setProperty name="messageBO" property="*" /><jsp:useBean id="messageBean" class="com.lyh.loginDao.MessageBean" scope="page"></jsp:useBean><%try{messageBean.getMessage(); messageBean.addMessage(); }catch(Exception e){ e.printStackTrace(); }%><html> <head> <base href="<%=basePath%>"> <title>add message into table</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"> </head> <body> <jsp:forward page="messageshow.jsp"/></body> </body></html>
@751638623: 我手头暂时没有java web的编译环境。你可以自己打出日志看看。
@蔡永吉: 哦, 谢谢你