⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatservlet.java

📁 一个用java+jsp实现的论坛和聊天室
💻 JAVA
字号:
/**
 * @author woexpert@yahoo.com
 * @version v0 110100
 */

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

public class ChatServlet extends HttpServlet {

  protected static final boolean DEBUG = false;

  // source acts as the distributor of new messages
  MessageSource source = new MessageSource();

  // doGet() returns the next message.  It blocks until there is one.
  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    if (DEBUG) System.out.println("in ChatServlet.doGet");

    // For properly handling Big5:
    //res.setContentType("text/plain");
    res.setContentType("text/plain; chatset=Big5");
    //PrintWriter out = res.getWriter();
    PrintWriter out = new PrintWriter(res.getOutputStream());

    // Return the next message (blocking)
    out.println(getNextMessage());

    out.close(); // added by woexpert
  }

  // doPost() accepts a new message and broadcasts it to all
  // the currently listening HTTP.
  public void doPost(HttpServletRequest req, HttpServletResponse res)
                                throws ServletException, IOException {
    if (DEBUG) System.out.println("in ChatServlet.doPost");

    // Accept the new message as the "message" parameter
    String message = req.getParameter("message");

    // Broadcast it to all listening clients
    if (message != null) broadcastMessage(message);

    // Set the status code to indicate there will be no response
    res.setStatus(res.SC_NO_CONTENT);
  }

  // getNextMessage() returns the next new message.
  // It blocks until there is one.
  public String getNextMessage() {
    if (DEBUG) System.out.println("in ChatServlet.getNextMessage");
    // Create a message sink to wait for a new message from the
    // message source.
    return new MessageSink().getNextMessage(source);
  }

  // broadcastMessage() informs all currently listening clients that there
  // is a new message.  Causes all calls to getNextMessage() to unblock.
  public void broadcastMessage(String message) {
    if (DEBUG) System.out.println("in ChatServlet.broadcastMessage");
    // Send the message to all the HTTP-connected clients by giving the
    // message to the message source
    source.sendMessage(message);
    
  }
}

// MessageSource acts as the source for new messages.
// Clients interested in receiving new messages can
// observe this object.
class MessageSource extends Observable {
  private static final boolean DEBUG = ChatServlet.DEBUG;
  public void sendMessage(String message) {
    if (DEBUG) System.out.println("in MessageSource.sendMessage");
    setChanged();
    notifyObservers(message);
  }
}

// MessageSink acts as the receiver of new messages.
// It listens to the source.
class MessageSink implements Observer {

  private static final boolean DEBUG = ChatServlet.DEBUG;
  String message = null;  // set by update() and read by getNextMessage()

  // Called by the message source when it gets a new message
  synchronized public void update(Observable o, Object arg) {
    if (DEBUG) System.out.println("in MessageSink.update");
    // Get the new message
    message = (String)arg;

    // Wake up our waiting thread
    notify();
  }

  // Gets the next message sent out from the message source
  synchronized public String getNextMessage(MessageSource source) {
    if (DEBUG) System.out.println("in MessageSink.getNextMessage");
    // Tell source we want to be told about new messages
    source.addObserver(this);

    // Wait until our update() method receives a message
    while (message == null) {
      try { wait(); } catch (Exception ignored) { }
    }

    // Tell source to stop telling us about new messages
    source.deleteObserver(this);

    // Now return the message we received
    // But first set the message instance variable to null
    // so update() and getNextMessage() can be called again.
    String messageCopy = message;
    message = null;
    return messageCopy;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -