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

📄 listusersinroomaction.java

📁 该源码是使用ajax技术实现的一个聊天室功能。如果想对程序进行修改
💻 JAVA
字号:
/*
 * Copyright 2005 Frank W. Zammetti
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.apache.struts.apps.ajaxchat.action;


import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.apps.ajaxchat.dao.AjaxChatDAO;
import org.apache.struts.apps.ajaxchat.dto.UserDTO;


/**
 * This is a Struts Action that is called via AJAX request to retrieve the list
 * of users in the room the user is currently in.
 *
 * @author <a href="mailto:frank.zammetti@pfpc.com">Frank W. Zammetti</a>.
 */
public class ListUsersInRoomAction extends Action {


  /**
   * Log instance.
   */
  private static Log log = LogFactory.getLog(ListUsersInRoomAction.class);


  /**
   * Execute.
   *
   * @param  mapping   ActionMapping.
   * @param  inForm    ActionForm.
   * @param  request   HttpServletRequest.
   * @param  response  HttpServletResponse.
   * @return           ActionForward.
   * @throws Exception If anything goes wrong.
   */
  public ActionForward execute(ActionMapping mapping, ActionForm inForm,
    HttpServletRequest request, HttpServletResponse response) throws Exception {

    log.debug("execute()...");

    HttpSession session = request.getSession();
    synchronized (session) {

      // Record this as the last AJAX request for the user.
      UserDTO user = (UserDTO)session.getAttribute("user");
      user.setLastAJAXRequest(new Date());
      session.setAttribute("user", user);

      // We'll need the name of the room to get a list for.
      String roomName = (String)request.getSession().getAttribute("roomName");
      log.debug("Getting user list for roomName = " + roomName);

      // Get user list and create a CSV list from it.
      AjaxChatDAO dao = AjaxChatDAO.getInstance();
      Vector userList = dao.getUserList(roomName);
      log.debug("userList = " + userList);
      StringBuffer xmlOut = new StringBuffer(1024);
      xmlOut.append("<users>");
      for (Iterator it = userList.iterator(); it.hasNext();) {
        UserDTO user1 = (UserDTO)it.next();
        xmlOut.append("<user name=\"" +
          StringEscapeUtils.escapeXml(user1.getUsername()) + "\"/>");
      }
      xmlOut.append("</users>");

      // Write out XML to response.
      log.debug(xmlOut);
      response.setContentType("text/xml");
      PrintWriter out = response.getWriter();
      out.println(xmlOut.toString());
      out.flush();

      // Response fully formed, nowhere to forward to.
      return null;

    }

  } // End execute().


} // End class.

⌨️ 快捷键说明

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