期中作业需要写一个留言板并且实现留言内容时间的输出
当时没有学servlet直接用JavaBean写的,并且没有数据库
index.jsp
<%@ page language="java" import="java.util.*,com.bean.*,java.text.SimpleDateFormat" pageEncoding="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>留言板</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">
-->
<script type="text/javascript">
function displayTime() {
var timeDiv = document.getElementById("timeDiv");
var nowTime = new Date();
var strNowTime = nowTime.toLocaleString();
timeDiv.innerHTML = strNowTime;
}
function start() {
window.setInterval("displayTime()", 1000)
}
function sub(){
if(document.getElementById("myname").value==""){
alert("姓名不能为空!");
return false;
}else if(document.getElementById("title1").value.length<5){
alert("留言标题不得小于5个字!");
return false;
}else if(document.getElementById("count").value.length<10){
alert("留言内容不得少于十个字!");
return false;
}
return true;
}
</script>
</head>
<body οnlοad="start();">
<div style="background-image: url('image/bg.jpg'); background-size:cover" >
<form method="post" name="index" action="MyJsp.jsp" οnsubmit="return sub();">
<center>
<table border="1" width="98%" height="98%" bordercolor="yellowgreen" cellspacing="0"
cellpadding="0">
<tr>
<td colspan="3">
<h1>
<center>
留言板
</center>
</h1>
</td>
</tr>
<tr>
<td rowspan="5" name="time">
<center><div id="timeDiv" name="time"></div></center>
</td>
<td>
姓 名:
<input type="text" name="myname" id="myname" placeholder="请输入姓名(必填)">
</td>
<td>
邮 箱:
<input type="text" name="email" id="email" value="请填写邮箱">
</td>
</tr>
<tr>
<td>
性 别:
<input type="radio" name="sex" id="sex" checked="checked" value="男">
男
<input type="radio" name="sex" id="sex" value="女" >
女
</td>
<td>
联系电话:
<input type="text" name="phone" id="phone" value="请填写电话号码">
</td>
</tr>
<tr>
<td>
留言标题:
<input type="text" name="title1" id="title1" placeholder="请输入标题不得少于五个字" style="width:200px" >
</td>
<td>
社交账号:
<input type="text" name="qq" id="qq" value="请输入社交账号 qq/微信">
</td>
</tr>
<tr>
<td colspan="2">
<p>留言内容:</p>
<p><textarea name="count" id="count" placeholder="请输入留言不得少于十个字" style="margin-left:80px; width:400px; height: 100px"></textarea></p>
<input type="submit" name="submit" value="提交" style="margin-left: 350px">
<input type="reset" name="reset" value="重置">
</td>
</tr>
</table>
</center>
</form>
</div>
</body>
</html>
myjsp.jsp
<%@ page language="java"
import="java.util.*,com.bean.*,java.util.Date,java.text.SimpleDateFormat"
pageEncoding="UTF-8"%>
<%@page import="java.io.Console"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</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 style="background-image: url('image/bg.jpg'); background-size:cover" >
<form method="post" name="form2">
<jsp:useBean id="word" class="com.bean.Word" scope="request"></jsp:useBean>
<jsp:setProperty property="*" name="word" />
<table width="98%" border="1" height="155" cellspacing="0"
cellpadding="0" align="center">
<tbody>
<tr><td colspan="8" align="center">留言列表</td></tr>
<tr align="center">
<td>
姓名
<br>
</td>
<td>
性别
<br>
</td>
<td>
联系电话
<br>
</td>
<td>
社交账号
</td>
<td>
邮箱
<br>
</td>
<td>
留言标题
<br>
</td>
<td>
留言内容
<br>
</td>
<td>
留言时间
<br>
</td>
</tr>
<%
ArrayList<Word> wordlist=(ArrayList<Word>) application
.getAttribute("wordlist");
System.out.println(wordlist==null);
if (wordlist==null){
wordlist=new ArrayList<Word>();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();
String time = simpleDateFormat.format(currentTime);
word.setTimeString(time);
wordlist.add(word);
application.setAttribute("wordlist",wordlist);
for (Word w:wordlist)
{
%>
<tr align="center">
<td>
<%=Tools.toChinese(w.getMyname())%>
</td>
<td>
<%=Tools.toChinese(w.getSex())%>
</td>
<td>
<%=Tools.toChinese(w.getPhone())%>
</td>
<td>
<%=Tools.toChinese(w.getQq())%>
</td>
<td>
<%=Tools.toChinese(w.getEmail())%>
</td>
<td>
<%=Tools.toChinese(w.getTitle1())%>
</td>
<td>
<%=Tools.toChinese(w.getCount())%>
</td>
<td>
<%=Tools.toChinese(w.getTimeString())%>
</td>
</tr>
<%
}
%>
</tbody>
</table>
<a href="index.jsp"><center>继续留言</center></a>
</form>
</body>
</html>
javabean中的内容
新建一个com.bean的包
Tools.java
package com.bean;
import java.io.UnsupportedEncodingException;
public class Tools {
static public String toChinese(String str){
try {
str = new String(str.getBytes("ISO-8859-1"),"Utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
static public int strToint(String str){
int i = 0;
if (str==null)
i = 0;
else {
i=Integer.parseInt(str);
}
return i;
}
}
word.java
package com.bean;
public class Word {
private String myname;
private String sex;
private String phone;
private String qq;
private String email;
private String title1;
private String count;
private String timeString;
public String getMyname() {
return myname;
}
public void setMyname(String myname) {
this.myname = myname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTitle1() {
return title1;
}
public void setTitle1(String title1) {
this.title1 = title1;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
public String getTimeString() {
return timeString;
}
public void setTimeString(String timeString) {
this.timeString = timeString;
}
}
自己加一个背景
运行结果为