topics.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 59 行

JAVA
59
字号
package forum;

import java.util.*;

/**
 * Represents all topics in the discussion forum.
 * <p>
 * In a real system there would be a database containing the topics.
 *
 * @author    Simon Brown
 */
public class Topics {

  /** a collection of all topics in the system */
  private static ArrayList topics = new ArrayList();

  /**
   * Populates the collection of all topics when the class is loaded.
   */
  static {
    Topic topic;
    Response response;
    User user1, user2;

    user1 = Users.getUser("Simon");
    user2 = Users.getUser("Sam");

    topic = new Topic(0, user1, "How do I write a Java Servlet?", "How do I write a Java Servlet and which class do I need to extend?");
    topic.add(new Response(user2, "You need to write a Java class that extends <code>javax.servlet.http.HttpServlet</code>, and implement the doGet or doPost methods."));
    topic.add(new Response(user1, "What's the purpose of doGet and doPost?"));
    topics.add(topic);

    topic = new Topic(1, user1, "Front Controller pattern?", "How do I use it?");
    topics.add(topic);

    topic = new Topic(2, user1, "HTTP session timeout", "When does the session timeout?");
    topics.add(topic);
  }

  /**
   * Gets a collection of all topics in the system.
   *
   * @return  a Collection of Topic instances
   */
  public static Collection getTopics() {
    return topics;
  }

  /**
   * Looks up the Topic with the specified id.
   *
   * @param id    the topic id
   * @return  the appropriate Topic instance
   */
  public static Topic getTopic(int id) {
    return (Topic)topics.get(id);
  }

}

⌨️ 快捷键说明

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