一、SpeakCountBean.javapackage chatroom;
import java.util.*;
public class SpeakCountBean {
  static int count;
  public static void SpeakCountBean() {
    count=0;
  }
  public static void setName(String name) {
    count++;
  }
  public static int getSpeakCount() {
    return count;
  }
} 二、SpeakBean.java
package chatroom;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SpeakBean {
   static String allSpeaking= new String("");
   static String CookieName = new String("");
   public SpeakBean() {
   }
   public static void setChat(String name,String speaking,
   HttpServletResponse response) {
      if (!name.equals("") && !speaking.equals("")) 
      allSpeaking=allSpeaking+name+"说:"+speaking+";
      Cookie cookie = new Cookie("name",name);
      response.addCookie(cookie);
   }
   public String getCookieName(HttpServletRequest request) {
      Cookie[] cookies = request.getCookies();
      if (cookies.length > 0) 
         {for (int i = 0; i < cookies.length; i++) 
            {Cookie requestcookie = cookies[i];
              if (requestcookie.getName().equals("name")) 
      CookieName = requestcookie.getValue();
            }
         } 
      return CookieName;
   }
   public String getAllSpeaking() {
      return allSpeaking;
   }
} 三、ChatRoom.java
package chatroom;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ChatRoom extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {chatroom.SpeakCountBean mycount=new chatroom.SpeakCountBean();
     mycount.setName(request.getParameter("name"));
     chatroom.SpeakBean myspeak=new chatroom.SpeakBean();
     if (mycount.getSpeakCount()>0) 
        myspeak.setChat(request.getParameter("name"),request.getParameter
("speaking"),response);
     getServletConfig().getServletContext().getRequestDispatcher
     ("/jsp/chatroom/chatroom.jsp").forward(request,response);
    }
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }
} 四、chatroom.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>最最简单的聊天室</title>
</head>
<h3>最最简单的聊天室</h3>
你讲了:
<jsp:useBean id="mycount" class="chatroom.SpeakCountBean" scope="session"/>
<%=mycount.getSpeakCount() %>
句话。<BR>
<jsp:useBean id="myspeak" class="chatroom.SpeakBean" scope="application"/>
<%=myspeak.getAllSpeaking() %>
<P>
<form action="http://localhost/examples/servlet/chatroom.ChatRoom" method=POST>
用户
<input type=text size=10 name=name value="<%=myspeak
.getCookieName(request) %>">
<br>
发言
<input type=text size=80 name=speaking>
<br>
<input type=submit value=提交>
</form>
</body>
</html> 这个结构很简单也很明确,MVC结构,用Servlet进行控制,很早以前就下到硬盘上了以为简单一直都没怎么研究过,最近无聊拉出来看看发现中间好几个细节都无法正确执行。  就象chatroom.jsp页面就无法显示出来,本来做了测试,如果将<%=myspeak
.getCookieName(request) %>代码取掉才可以正常显示出来,但是无法实现正常的功能。