JSP实验(6) 用javabean制作一个留言板
一、实验题目及要求
实验目的
本实验的目的是练习通过JavaBean实现留言板功能。
实验要求
要求编写两个JSP页面:inputMess.jsp和show.jsp。编写一个名字为board的Javabean,其中board由MessBoard.class类负责创建。
1.inputMess.jsp的具体要求
input.jsp页面提供一个表单。其中表单允许用户输入留言者的姓名、留言标题和留言内容,该表单将用户输入的信息提交给当前页面,当前页面调用名字为board的bean,并使用表单提交的数据设置board的有关属性的值。要求在inputMess.jsp提供一个超链接,以便用户单击这个超链接访问show.jsp页面。
2.show.jsp的具体要求
show.jsp调用名字为board的bean,并显示该bean的allMessage属性的值。
3.MessBoard.java的具体要求
编写的MessBoard.java应当有刻画留言者的姓名、留言标题和留言内容属性,并且有刻画全部留言信息的属性:allMessage。
实验效果
二、实验代码
1.inputMess.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: ruochen
Date: 2020/11/16
Time: 14:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="messboard" class="tang.ruochen.shuai.messboard" scope="application"/>
<html>
<head>
<title>留言板</title>
</head>
<body>
<pre>
<form action="show.jsp" method="post">
输入你的名字:
<INPUT type="text" name="name">
输入你的留言标题:
<INPUT type="text" name="title">
输入你的留言内容:
<TEXTAREA name="content"></TEXTAREA>
<INPUT type="submit" value="提交">
</form>
<a href="show.jsp">看留言板</a>
</pre>
</body>
</html>
2.show.jsp
代码如下(示例):
<%--
Created by IntelliJ IDEA.
User: ruochen
Date: 2020/11/16
Time: 14:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="show" class="tang.ruochen.shuai.messboard" scope="application"/>
<html>
<head>
<title>留言板信息</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String title = request.getParameter("title");
String content = request.getParameter("content");
%>
<jsp:setProperty name="show" property="name" value="<%=name%>"/>
<jsp:setProperty name="show" property="title" value="<%=title%>"/>
<jsp:setProperty name="show" property="content" value="<%=content%>"/>
<jsp:getProperty name="show" property="result"/>
<a href="inputmess_2.jsp">去留言</a>
</body>
</html>
3 messbord.java
代码如下(示例):
package tang.ruochen.shuai;
import javax.xml.crypto.Data;
import java.sql.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;
public class messboard {
String name;
String title;
String content;
String datatime;
StringBuffer result;
public void setName(String name) {
this.name = name;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public messboard()throws Exception {
result=new StringBuffer();
Class.forName("com.mysql.cj.jdbc.Driver");
}
public String getResult() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
this.datatime = sdf.format(now);
//this.datatime = (new Timestamp(new Date().getTime())).toString();
result.setLength(0);
String condition="insert into textnote values("+"'"+name+"','"+title+"','"+content+"','"+datatime+"');";
//String condition="insert into car values("+"'"+name+"','"+title+"','"+content+"');";
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/myjdbc?" + "user=root&password=&characterEncoding=UTF-8&serverTimezone=GMT");
Statement sql = con.createStatement(TYPE_SCROLL_INSENSITIVE,CONCUR_READ_ONLY);
ResultSet rs;
if(!(name==null||title==null||content==null))
sql.executeUpdate(condition);
rs=sql.executeQuery("select * from textnote;");
ResultSetMetaData rsmd = rs.getMetaData();
int n = rsmd.getColumnCount();
// rs.last();
// result.append("目前已登记车辆共"+rs.getRow()+"辆:");
// rs.beforeFirst();
result.append("<table border = 1>");
result.append("<tr>");
result.append("<th>"+"留言者姓名"+"</th>");
result.append("<th>"+"留言标题"+"</th>");
result.append("<th>"+"留言内容"+"</th>");
result.append("<th>"+"留言时间"+"</th>");
result.append("</tr>");
while(rs.next()){
result.append("<tr>");
for(int i = 1;i<=n;i++)
{
result.append("<td>"+rs.getString(i)+"</td>");
}
result.append("</tr>");
}
result.append("</table>");
con.close();
}
catch (SQLException e){
result.append(e.getMessage());
}
return result.toString();
}
}
效果演示