日期:2014-05-17  浏览次数:20724 次

许多问题,关于jsp搭建一个简易聊天室
如题,设计思想是从页面用表单提交到servlet,在servlet上接收,读取一个application对象(如果没有就创建),之后再把更新好的application对象存到一个javabean内,使前段的jsp调用这个javabean的代码找到历史聊天记录并输出,设计是这样的,但问题非常多,首先application对象无论执行几次都是每次执行时就新创建,说明一直没有创建成功。第二,读到JSP页面上不知道如何输出到文本域内,求助各位大神。代码如下
前台jsp部分

<%@ page language="java" contentType="text/html; charset=utf-8" import="sjw.pratice.TransferInfo"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>聊天室</title>
</head>
<script type="text/javascript">
var info=Record;
var f=document.getElementById("form1");
f.textbox1.value=info;
</script>
<body>
<form id="form1" name="form1" method="post" action="ReceiveInfo">
  <table width=888 height="466" border="3" cellpadding="3" cellspacing="3">
    <tr align="center">
      <td height=375 colspan="2"><textarea name="textbox1" cols="103" rows="28" readonly="readonly" id="textbox1"></textarea></td>
      <td width=151 rowspan="2"><textarea name="textbox3" cols="15" rows="40" readonly="readonly" id="textbox3"></textarea></textarea></td>
    </tr>
    <tr>
      <td width=656 height="76"><textarea name="textbox2" id="textbox2" cols="90" rows="5"></textarea></td>
      <td width=71 align="center"><input type="submit" name="button1" id="button1" value="发送" /></td>
    </tr>
  </table>
</form>
<%!String Record=TransferInfo.getchattingRecord(); %>
</body>
</html>

servlet部分

package sjw.pratice;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

@WebServlet("/ReceiveInfo")
public class ReceiveInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
    public ReceiveInfo() {
        super();
    }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException {
String record=new String();
ServletContext application=super.getServletContext();
Calendar date=Calendar.getInstance();