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

📄 postmessageaction.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.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.action.DynaActionForm;
import org.apache.struts.apps.ajaxchat.dao.AjaxChatDAO;
import org.apache.struts.apps.ajaxchat.dto.MessageDTO;
import org.apache.struts.apps.ajaxchat.dto.UserDTO;


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


  /**
   * Log instance.
   */
  private static Log log = LogFactory.getLog(PostMessageAction.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) {

      // Get the users' name, message text and room they are in, and record
      // this as the last AJAX request for the user.
      UserDTO user = (UserDTO)session.getAttribute("user");
      user.setLastAJAXRequest(new Date());
      session.setAttribute("user", user);
      String username = user.getUsername();
      String roomName = (String)session.getAttribute("roomName");
      DynaActionForm form = (DynaActionForm)inForm;
      //zl modified !
     
      
      /*
      Update the file $CATALINA_HOME/conf/server.xml for UTF-8 support by connectors. Example: 

    	  <Connector port="8080" 

    	  maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/> 
      */
      //String msgText = new String(((String)form.get("msgText")).getBytes("ISO8859-1"),"UTF-8");
//    String msgText = new String(((String)form.get("msgText")).getBytes("GBK"),"UTF-8"); "我是我" 有问题
      String msgText = (String)form.get("msgText");
      // Create a new MessageDTO and post it.
      MessageDTO message = new MessageDTO();
      message.setPostedBy(user);
      message.setPostedDateTime(new Date());
      message.setText(msgText);
      AjaxChatDAO dao = AjaxChatDAO.getInstance();
      dao.postMessage(roomName, message);

      // There isn't actually anything to return from this function.
      return null;

    }

  } // End execute().


} // End class.

⌨️ 快捷键说明

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