日期:2014-05-18  浏览次数:20671 次

模仿QQ聊天用jsp做了一个简单的聊天程序遇到的几个小问题!有兴趣者入!
我写了一个聊天的小程序,主界面是一个frameset,包含了三个页面:input.jsp(用来让用户输入聊天信息),chatSpace.jsp(用来显示聊天信息),user.jsp(显示在线用户)。

问题1:
input.jsp里用到了一个表单text来提交聊天信息,如何在用户发送该信息后,让text域清空呢?我试了用一个javaScript函数作前台验证,当用户点下submit时,onclick“erase()”,但是这样的话,text中聊天信息就传不到chatSpace.jsp里了!怎样才能在chatSpace.jsp文件接受到text里的值后才将text域清空呢?

问题2:
因为是一个frameset,chatSpace.jsp里边有个滚动条,但是每次用户提交聊天信息以后,chatSpace.jsp里的滚动条都会跳到最上端,要阅读聊天信息还得往下拉,很不方便,怎样才能让滚动条一直在最下方呢?就像QQ似的?



------解决方案--------------------

问题1:
在text控件中加个键盘事件,针对敲回车时触发submit

------解决方案--------------------
package liaotian;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MessageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";

//Initialize global variables
public void init() throws ServletException {
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setCharacterEncoding( "GBK ");
request.setCharacterEncoding( "GBK ");
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <title> MessageServlet </title> </head> ");
out.println( " <body bgcolor=\ "#ffffff\ "> ");
String strMsg = request.getParameter( "message ");
if (strMsg==null||strMsg.equals( " "))
{
displayHtml(out);
}
else
{
ServletContext app = getServletContext();
StringBuffer objMsg =(StringBuffer)app.getAttribute( "objMessage ");
if (objMsg==null)
{
objMsg = new StringBuffer();
}
String name = request.getParameter( "name ");
objMsg.append( " <b> "+name+ "说: </b> "+strMsg+ " <br> ");
app.setAttribute( "objMessage ",objMsg);
displayHtml(out);
}

out.close();
}
public void displayHtml(PrintWriter out)
{
out.println( " <html> ");
out.println( " <head> <title> MessageServlet </title> </head> ");
out.println( " <body bgcolor=#ffffff onload = javascript:frm.message.focus()> ");
out.println( " <form method = post name =frm> ");
out.println( " <input type = text name = message> ");
out.println( " <input type=submit value = '发送 '> ");
out.println( " </form> ");
out.println( " </body> ");
out.println( " </html> ");
}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//Clean up r