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

📄 servletlistserver.java

📁 java网络编程方面的源码,其中有一个整合的聊天室,比较不错,建议大家下载练习,配合java网络编程技术内幕看
💻 JAVA
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletListServer extends PersistentHttpServlet {
  protected IDList idList;
  
  public void init (ServletConfig config) throws ServletException {
    String period = config.getInitParameter ("persistencePeriod");
    super.init (config, (period == null) ? -1 :
                Integer.parseInt (period));
    idList = (IDList) state;
  }

  protected Serializable createState () {
    return new IDList ();
  }

  public void doGet (HttpServletRequest request,
                     HttpServletResponse response) throws IOException {
    String command = request.getPathInfo ();
    if (command != null)
      command = command.substring (1);
    if (command == null) {
      String name = request.getServletPath ();
      name = name.substring (name.lastIndexOf ('/'));
      response.setContentType ("text/html");
      PrintWriter writer = response.getWriter ();
      writer.println
        ("<html><head><title>" + name + " status</title></head><body>");
      writer.println ("<h2>" + name + " is alive</h2>");
      writer.println ("</body></html");
      writer.close ();
    } else if (command.equals ("getUpdate")) {
      getUpdate (request, response);
    } else {
      response.sendError (HttpServletResponse.SC_BAD_REQUEST,
                          command + " not supported");
    }
  }

  protected void getUpdate (HttpServletRequest request, HttpServletResponse response) throws IOException {
    String count = request.getParameter ("count");
    IDList update = null;
    try {
      if (Integer.parseInt (count) != idList.getUpdateCount ())
        update = (IDList) idList.clone ();
    } catch (NumberFormatException ignored) {
    }
    response.setContentType ("application/octet-stream");
    ObjectOutputStream out = new ObjectOutputStream (
      response.getOutputStream ());
    out.writeObject (update);
    out.close ();
  }

  public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String command = request.getPathInfo ();
    if (command != null)
      command = command.substring (1);
    try {
      if (command == null) {
        response.sendError (HttpServletResponse.SC_BAD_REQUEST,
                            "null command not supported");
      } else if (command.equals ("addElement")) {
        addElement (request, response);
      } else if (command.equals ("updateElement")) {
        updateElement (request, response);
      } else if (command.equals ("replaceElement")) {
        replaceElement (request, response);
      } else if (command.equals ("removeElement")) {
        removeElement (request, response);
      } else {
        response.sendError (HttpServletResponse.SC_BAD_REQUEST,
                            command + " not supported");
      }
    } catch (ClassNotFoundException ex) {
      response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

  protected void addElement (HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException {
    ObjectInputStream in =
      new ObjectInputStream (request.getInputStream ());
    Object element = in.readObject ();
    ID id = idList.allocateID ();
    response.setContentType ("application/octet-stream");
    ObjectOutputStream out =
      new ObjectOutputStream (response.getOutputStream ());
    if (idList.addElement (id, element)) {
      stateChanged ();
      out.writeObject (id);
    } else {
      out.writeObject (null);
    }
    out.close ();
  }

  protected void updateElement (HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException {
    ObjectInputStream in =
      new ObjectInputStream (request.getInputStream ());
    ID id = (ID) in.readObject ();
    Object element = in.readObject ();
    ID newID = idList.allocateID ();
    response.setContentType ("application/octet-stream");
    ObjectOutputStream out =
      new ObjectOutputStream (response.getOutputStream ());
    if (idList.updateElement (id, newID, element)) {
      stateChanged ();
      out.writeObject (newID);
    } else {
      out.writeObject (null);
    }
    out.close ();
  }

  protected void replaceElement (HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException {
    ObjectInputStream in =
      new ObjectInputStream (request.getInputStream ());
    ID id = (ID) in.readObject ();
    Object element = in.readObject ();
    ID newID = idList.allocateID ();
    response.setContentType ("application/octet-stream");
    ObjectOutputStream out =
      new ObjectOutputStream (response.getOutputStream ());
    if (idList.replaceElement (id, newID, element)) {
      stateChanged ();
      out.writeObject (newID);
    } else {
      out.writeObject (null);
    }
    out.close ();
  }

  protected void removeElement (HttpServletRequest request, HttpServletResponse response) throws IOException, ClassNotFoundException {
    ObjectInputStream in =
      new ObjectInputStream (request.getInputStream ());
    ID id = (ID) in.readObject ();
    response.setContentType ("application/octet-stream");
    ObjectOutputStream out =
      new ObjectOutputStream (response.getOutputStream ());
    if (idList.removeElement (id)) {
      stateChanged ();
      out.writeBoolean (true);
    } else {
      out.writeBoolean (false);
    }
    out.close ();
  }
}

⌨️ 快捷键说明

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