首页 新闻 会员 周边 捐助

各位大神帮我看看怎么不能显示所有访客留言啊(我是用jsp+javaBean开发的萌新勿喷)还有怎么贴代码啊要什么工具吗

0
悬赏园豆:100 [已关闭问题] 关闭于 2018-01-05 18:35

(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>

古叶丶扁舟的主页 古叶丶扁舟 | 初学一级 | 园豆:9
提问于:2016-12-20 23:10
< >
分享
所有回答(2)
0

添加html代码呀,没有高亮很难看的

silianpan | 园豆:571 (小虾三级) | 2016-12-21 10:05
0
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&amp;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;

}
}
 
View Code
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;
}
}
View Code
<%@ 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>
View Code
<%@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>
View Code
<%@ 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>
View Code

首先,格式化下楼主代码。回答我放在评论里。

 

CaiYongji | 园豆:1267 (小虾三级) | 2016-12-21 10:37
  • bean和bo的文件命名反了。就是说bean应该是get set 属性类。 bo是执行业务逻辑的(理论上需要dao层来实现MVC)。
  • 在执行messageBean.getMessage();messageBean.addMessage();时存在逻辑错误。addMessage最好使用addMessage(MessageBo msg)而不是全局变量。否则插入数据库时很可能出现空值。
  • 遍历显示“留言”没有问题,但是最好使用AJAX异步获取留言,以防止留言数量过大导致响应时间过长。

 

支持(0) 反对(0) CaiYongji | 园豆:1267 (小虾三级) | 2016-12-21 10:53

@蔡永吉: 我才刚学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>

支持(1) 反对(0) 古叶丶扁舟 | 园豆:9 (初学一级) | 2016-12-21 12:21

@751638623: 我手头暂时没有java web的编译环境。你可以自己打出日志看看。

 

  • JAVA System.out.println();
  • JSP console.log();
支持(0) 反对(0) CaiYongji | 园豆:1267 (小虾三级) | 2016-12-21 13:23

@蔡永吉: 哦, 谢谢你

支持(0) 反对(0) 古叶丶扁舟 | 园豆:9 (初学一级) | 2016-12-21 13:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册