本文最后更新于 3293 天前,其中的信息可能已经有所发展或是发生改变。
一个简易的非实时聊天版的jsp,无CSS。新手练习,高手路过请无视。
其实这是方便我明天上课交作业而已,毕竟U盘不见了。U盘上的种子、毛片都不见了。所以这几天想起来我的片片,心里几十个草泥马路过呢,数了数,我这是第几次丢U盘了。算了算了,说正事三种方法的聊天代码
下面是方法一,是用java类实现:
package mypackage;
import java.util.Date;
//定义一个数据类,用于存放用户提交的信息
public class UserMessage {
private String userName="";//用户名
private String title=""; //标题
private String content=""; //留言内容
private Date messTime=null;//留言时间
//构造方法
public UserMessage(String userName, String title, String content) {
//把传进来的参数赋给类的相应变量
this.userName = userName;
this.title = title;
this.content = content;
//设置时间
setMessTime();
}
public String getUserName() {
return userName;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public Date getMessTime() {
return messTime;
}
private void setMessTime() {
this.messTime = new Date();
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%request.setCharacterEncoding("GB18030");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>简易留言板</title>
<script type="text/javascript">
function checkDatas()
{
if(document.getElementById("title").value==""){
alert("标题不能为空!");
document.form1.title.select();
return false;
}
if(document.getElementById("userName").value==""){
alert("发言人不能为空!");
document.form1.userName.select();
return false;
}
if(document.getElementById("messages").value==""){
alert("留言内容不能为空!");
document.form1.messages.select();
return false;
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="messagePane.jsp" onsubmit="return checkDatas()">
<CENTER><B>简易留言板</B></CENTER><HR>
<table border="1" align="center">
<tr>
<td width="100" align="center">标题</td>
<td><input type="text" id="title" name="title" size="30"></td>
</tr>
<tr>
<td width="100" align="center">发言人</td>
<td><input type="text" id="userName" name="userName" size="30"></td>
</tr>
<tr>
<td width="100" align="center">留言内容</td>
<td><textarea id="messages" name="messages" rows="5" cols="28"></textarea></td>
</tr>
<tr>
<td align="center" colspan="2"><input name="submit" type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
<%@page language="java" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.util.*"%>
<%request.setCharacterEncoding("GB18030");%>
<html>
<head>
<title>获取并保存留言</title>
</head>
<body>
<%
//S1:取用户提交的参数
String strUserName = request.getParameter("userName");//发言人
String strTitle = request.getParameter("title");//标题
String strMessages = request.getParameter("messages");//留言内容
//S2:创建消息对象aMessage
UserMessage aMessage = new UserMessage(strUserName, strTitle,strMessages);
//--------S3:保存留言到向量中---------------
@SuppressWarnings("unchecked")//忽略代码中的警告
Vector<UserMessage> v = (Vector<UserMessage>) application.getAttribute("Mess");//取出对象Mess中的数据放到消息容器v中
//向量v的作用是存放留言数据。
if (v == null) {//如果是空的,那么创建一个。(说明是第一次添加留言)
v = new Vector<UserMessage>();//创建一个Vector对象v
}
v.add(aMessage);//把留言项放进容器v
//S4:将向量信息放进application对象中
application.setAttribute("Mess", v);//把v放到application
//S5:输出html
out.print("您的留言已提交,感谢您的配合!<br/>");
out.print("<a href='Ex3_8.jsp'>还有话说?</a>"+"<br/>");
out.print("<a href='showMessage.jsp'>查看所有留言信息</a>");
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
request.setCharacterEncoding("GB18030");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>查看留言</title>
</head>
<body>
<%
@SuppressWarnings("unchecked")
//忽略代码中的警告
Vector<UserMessage> v = (Vector<UserMessage>) application.getAttribute("Mess");////取出对象Mess中的数据放到消息容器v
if (v == null) {//如果容器是空对象(说明还没有创建,即还没有人留言)
out.print("暂时没有留言!");//输出html提示
return;//退出
}
//循环,取出容器里的各项留言并输出
for (int i = 0; i < v.size(); i++) {
UserMessage aMessage = v.elementAt(i);//取出第i个留言项
String userName = aMessage.getUserName();//取出留言项里的用户名
String title = aMessage.getTitle();//取出留言项里的标题
String content = aMessage.getContent();//取出留言项里的留言内容
Date theDate = aMessage.getMessTime();//取出留言项里的时间
//把Date类型的时间,转换成String类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//定义输出格式
String strDate = sdf.format(theDate);//把格式化后的日期(字符类型),赋给strDate
//输出一个留言项
out.print("<hr>");
out.print("<table width='700' border='0' cellspacing='0' cellpadding='1'>");
out.print("<tr>");
out.print("<td width='395'>序号:" + (i + 1) + " 留言人:" + userName+ "</td>");
out.print("<td width='301'>日期:" + strDate + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>标题:" + title + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>内容:</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>" + content + "</td>");
out.print("</tr>");
out.print("</table>");
}
%>
</body>
</html>
方法二是使用Vector的构造函数实现:
<%@page language="java" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.util.*"%>
<%request.setCharacterEncoding("GB18030");%>
<html>
<head>
<title>获取并保存留言</title>
</head>
<body>
<%
//S1:取用户提交的参数
String strUserName = request.getParameter("userName");//发言人
String strTitle = request.getParameter("title");//标题
String strMessages = request.getParameter("messages");//留言内容
//S2:创建消息对象aMessage
String strInto = strUserName+"-"+strTitle+"-"+strMessages;
//--------S3:保存留言到向量中---------------
@SuppressWarnings("unchecked")//忽略代码中的警告
List leaveList = (ArrayList<String>) application.getAttribute("Mess");
if (leaveList == null) {//如果是空的,那么创建一个。(说明是第一次添加留言)
leaveList = new ArrayList<String>();//创建一个ArrayList对象leaveList
}
leaveList.add(strInto);//把留言项放进leaveList
//S4:将向量信息放进application对象中
application.setAttribute("Mess", leaveList);//把leaveList放到application
//S5:输出html
out.print("您的留言已提交,感谢您的配合!<br/>");
out.print("<a href='Ex3_8_1.jsp'>还有话说?</a>"+"<br/>");
out.print("<a href='showMessage_1.jsp'>查看所有留言信息</a>");
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
request.setCharacterEncoding("GB18030");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>查看留言</title>
</head>
<body>
<%
@SuppressWarnings("unchecked")
//忽略代码中的警告
List leaveList = (ArrayList<String>) application.getAttribute("Mess");////取出对象Mess中的数据放到消息容器v
if (leaveList == null) {//如果容器是空对象(说明还没有创建,即还没有人留言)
out.print("暂时没有留言!");//输出html提示
return;//退出
}
//循环,取出容器里的各项留言并输出
for (int i = 0; i < leaveList.size(); i++) {
String strAll[] = ((String)leaveList.get(i)).split("-");
Date theDate = new Date();//取出留言项里的时间
//把Date类型的时间,转换成String类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//定义输出格式
String strDate = sdf.format(theDate);//把格式化后的日期(字符类型),赋给strDate
//输出一个留言项
out.print("<hr>");
out.print("<table width='700' border='0' cellspacing='0' cellpadding='1'>");
out.print("<tr>");
out.print("<td width='395'>序号:" + (i + 1) + " 留言人:" + strAll[0]+ "</td>");
out.print("<td width='301'>日期:" + strDate + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>标题:" + strAll[1] + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>内容:</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>" + strAll[2] + "</td>");
out.print("</tr>");
out.print("</table>");
}
%>
</body>
</html>
方法三是用迭代器实现:
<%@page language="java" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.util.*"%>
<%request.setCharacterEncoding("GB18030");%>
<html>
<head>
<title>获取并保存留言</title>
</head>
<body>
<%
//S1:取用户提交的参数
String strUserName = request.getParameter("userName");//发言人
String strTitle = request.getParameter("title");//标题
String strMessages = request.getParameter("messages");//留言内容
//S2:创建消息对象aMessage
//String strInto = strUserName+"-"+strTitle+"-"+strMessages;
//--------S3:保存留言到向量中---------------
@SuppressWarnings("unchecked")//忽略代码中的警告
List leaveList = (ArrayList<String>) application.getAttribute("Mess");//取出对象Mess中的数据放到消息容器v中
//向量v的作用是存放留言数据。
if (leaveList == null) {//如果是空的,那么创建一个。(说明是第一次添加留言)
leaveList = new ArrayList<String>();//创建一个字符串ArrayList对象leaveList
}
leaveList.add(strUserName);//把留言项放进容器leaveList
leaveList.add(strTitle);
leaveList.add(strMessages);
//S4:将向量信息放进application对象中
application.setAttribute("Mess", leaveList);//把v放到application
//S5:输出html
out.print("您的留言已提交,感谢您的配合!<br/>");
out.print("<a href='Ex3_8_2.jsp'>还有话说?</a>"+"<br/>");
out.print("<a href='showMessage_2.jsp'>查看所有留言信息</a>");
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="mypackage.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%
request.setCharacterEncoding("GB18030");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>查看留言</title>
</head>
<body>
<%
@SuppressWarnings("unchecked")
//忽略代码中的警告
List leaveList = (ArrayList<String>) application.getAttribute("Mess");////取出对象Mess中的数据放到消息容器v
if (leaveList == null) {//如果容器是空对象(说明还没有创建,即还没有人留言)
out.print("暂时没有留言!");//输出html提示
return;//退出
}
Iterator<String> ite = leaveList.iterator();//利用迭代器Iterator进行显示结果,模拟指针对链表进行操作
int i = 0;
while (ite.hasNext()) {
String into = ite.next();
i++;
String datetime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime()); //获取系统时间
//Date theDate = new Date();//取出留言项里的时间
//把Date类型的时间,转换成String类型
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//定义输出格式
// String strDate = sdf.format(theDate);//把格式化后的日期(字符类型),赋给strDate
//输出一个留言项
out.print("<hr>");
out.print("<table width='700' border='0' cellspacing='0' cellpadding='1'>");
out.print("<tr>");
out.print("<td width='395'>序号:" + i + " 留言人:" + into+ "</td>");
out.print("<td width='301'>日期:" + datetime + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>标题:" + into + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>内容:</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td colspan='2'>" + into + "</td>");
out.print("</tr>");
out.print("</table>");
}
%>
<a href='Ex3_8_2.jsp'>返回去继续吐槽</a>
</body>
</html>