示例#1
0
  private void perform(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String title = request.getParameter("title");
    String content = request.getParameter("content");

    Topic topic = new Topic();
    topic.setTitle(title);
    topic.setContent(content);

    BlogController ctrl = BlogController.getInstance();
    ctrl.postTopic(topic);

    request.getRequestDispatcher("/read").forward(request, response);
  }
  private Topic readTopic(int topicID) throws IOException {

    Topic topic = new Topic();

    record = new String();
    String topicString = "";
    boolean topicFounded = false;
    while ((record = br.readLine()) != null) {

      // System.out.println(record);
      if (record.indexOf("<num>") != -1
          && record.indexOf("Number:") != -1
          && record.indexOf(String.valueOf(topicID)) != -1) {
        topicFounded = true;
      }

      if (topicFounded) {
        topicString += record;
        if (record.indexOf("</top>") != -1) {

          // System.out.println(topicString);
          int titlePos = topicString.indexOf("<title>");
          int descPos = topicString.indexOf("<desc>");
          int narrPos = topicString.indexOf("<narr>");

          String title =
              topicString.substring(titlePos + 7, descPos).replaceFirst("Topic:", "").trim();

          String desc =
              topicString.substring(descPos + 6, narrPos).replaceFirst("Description:", "").trim();
          String narr =
              topicString
                  .substring(narrPos + 6, topicString.length() - 6)
                  .replaceFirst("Narrative:", "")
                  .trim();

          topic.setId(topicID);
          topic.setTitle(title);
          topic.setDescription(desc);
          topic.setNarrative(narr);

          return topic;
        }
      }
    }

    return null;
  }