我写了一个聊天的小程序,主界面是一个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=null;滚动条就不知道了。
      

  2.   

    将text=null,显示效果是在文本域中显示一个"null"单词啊!而不是什么也没有!
      

  3.   


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

  4.   

    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 resources
        public void destroy() {
        }
    }
    就这样
      

  5.   

    滚动条的问题:
    把下面的代码放在页面的最下面
    <script>
    window.scroll(0,document.body.scrollHeight);
    </script>
      

  6.   

    呵呵,用AJAX做吧,我去年做过一个
      

  7.   

    回复Gavinsky_feifei() :你用的两个displayHtml(out)调用结果不都是一样的吗?String name = request.getParameter("name");
    objMsg.append("<b>"+name+"说:</b>"+strMsg+"<br/>");
    app.setAttribute("objMessage",objMsg);
    displayHtml(out);
    这段代码和
    if (strMsg==null||strMsg.equals(""))
    {
    displayHtml(out);
    }的输出效果不是一样的吗?