ajax聊天室(JSP版)5

原创 2007年09月19日 21:18:00

用来的Java类,除了上述几个外,就剩下以下几个了。

两个过滤器类,一个用来过滤字符,一个用来过滤未登录的用户。


package org.jvk.chatroom.filter;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.jvk.chatroom.servlet.BaseServlet;
 
public class ChatroomFilter implements Filter {
 
    private static final String LOGIN_PAGE = "/chatroom";
 
    private static final String USER = BaseServlet.USER;
 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
           throws IOException, ServletException {
       HttpServletRequest req = (HttpServletRequest) request;
       HttpSession session = req.getSession(false);
       if (session == null || session.getAttribute(USER) == null) {
           ((HttpServletResponse) response).sendRedirect(LOGIN_PAGE);
           return;
       }
       chain.doFilter(request, response);
    }
 
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void destroy() {
    }
 }


package org.jvk.chatroom.filter;
 
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class CharacterEncoding implements Filter {
 
    protected String encoding = null;
 
    protected FilterConfig filterConfig = null;
 
    protected booleanignore = true;
 
    public void destroy() {
 
       this.encoding = null;
       this.filterConfig = null;
 
    }
 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
           ServletException {
 
       if (ignore || (request.getCharacterEncoding() == null)) {
           String encoding = selectEncoding(request);
           if (encoding != null)
              request.setCharacterEncoding(encoding);
       }
 
       chain.doFilter(request, response);
 
    }
    public void init(FilterConfig filterConfig) throws ServletException {
 
       this.filterConfig = filterConfig;
       this.encoding = filterConfig.getInitParameter("encoding");
       String value = filterConfig.getInitParameter("ignore");
       if (value == null)
           this.ignore = true;
       elseif (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"))
           this.ignore = true;
       else
           this.ignore = false;
 
    }
 
   protected String selectEncoding(ServletRequest request) {
 
       return (this.encoding);
 
    }
 
}
 

一个Listener,用于初始化聊天室(调用ChatroomManager的config方法)。
 
package org.jvk.chatroom.listener;
 
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jvk.chatroom.service.ChatroomManager;
 
public class InitChatroom implements ServletContextListener {
    private static Log log = LogFactory.getLog(InitChatroom.class);
 
    public void contextInitialized(ServletContextEvent event) {
       String configFile = event.getServletContext().getInitParameter("configFile");
       try {
           ChatroomManager.newInstance().config(new File(event.getServletContext().getRealPath(configFile)));
       } catch (IOException e) {
           log.error(e);
           throw new RuntimeException(e.getMessage());
       }
    }
    public void contextDestroyed(ServletContextEvent event) {
    }
}

至此,所有的Java类代码都没有了。下面开始写客户端的JS代码和界面的HTML代码。
顺便将配置文件也贴出来。

<?xml version="1.0" encoding="GBK"?>
<projectname="ajax聊天室"default="default"basedir="E:/jsp/chatroom">
    <!-- 设置路径属性   -->
    <propertyenvironment="env"/>
    <!--
    <property file="build.properties" />
    -->
    <propertyname="tomcat.home"value="G:/GreenSoftware/tomcat-6.0.10"/>
    <propertyname="source.root"value="${basedir}/src"/>
    <propertyname="class.root"value="${source.root}/web/WEB-INF/classes"/>
    <propertyname="web.lib"value="${source.root}/web/WEB-INF/lib"/>
    <propertyname="tomcat.lib"value="${tomcat.home}/lib"/>
    <propertyname="dist"value="${basedir}/dist"/>
    <propertyname="appName"value="chatroom"/>
    <!-- 设置类文件路径 -->
    <pathid="classpath">
       <!-- 包括自己编写的类 -->
       <pathelementlocation="${class.root}"/>
       <!-- 包括Tomcat里的 servlet-api.jar jsp-api.jar -->
       <filesetdir="${tomcat.lib}">
           <includename="servlet-api.jar"/>
           <includename="jsp-api.jar"/>
       </fileset>
       <filesetdir="${web.lib}">
           <includename="*.jar"/>
       </fileset>
    </path>
    <!-- 清理文件 -->
    <targetname="clean">
       <deletedir="${class.root}"/>
       <deletedir="${dist}"/>
    </target>
 
    <!-- 复制文件到classes目录下 -->
    <targetname="prepare">
       <mkdirdir="${dist}"/>
       <copytodir="${class.root}">
           <filesetdir="${source.root}/java">
              <excludename="**/*.java"/>
           </fileset>
       </copy>
    </target>
 
    <!-- 编译Java源文件 -->
    <targetname="compile"depends="prepare">
       <javacclasspathref="classpath"srcdir="${source.root}/java"destdir="${class.root}"/>
    </target>
    <targetname="c"depends="compile"/>
    <!-- 打包成war文件 -->
    <targetname="war"depends="compile">
       <jarjarfile="${dist}/${appName}.war"basedir="${source.root}/web"/>
    </target>
    <targetname="zip"depends="war">
       <zipzipfile="${dist}/${appName}_bin_src.zip">
           <filesetdir="${dist}">
              <includename="${appName}.war"/>
           </fileset>
           <filesetdir="${basedir}">
              <includename="src/**"/>
              <includename="build.xml"/>
           </fileset>
       </zip>
    </target>
 
    <!-- 生成API文档 -->
    <targetname="doc">
       <javadocpackagenames="*"sourcepath="${source.root}/java"defaultexcludes="yes"destdir="${dist}/doc"author="true"version="true"use="true"windowtitle="${ant.project.name}"/>
    </target>
 
    <!-- 启动Tomcat服务器 -->
    <targetname="run.tomcat">
       <execdir="${tomcat.home}/bin"executable="cmd.exe">
           <argline="/c startup.bat"/>
       </exec>
    </target>
    <!-- 关闭Tomcat服务器 -->
    <targetname="close.tomcat">
       <execdir="${tomcat.home}/bin"executable="cmd.exe">
           <argline="/c shutdown.bat"/>
       </exec>
    </target>
    <!-- 部署到Tomcat服务器 -->
    <targetname="deploy"depends="war">
       <deletedir="${tomcat.home}/webapps/${appName}"/>
       <copyfile="${dist}/${appName}.war"overwrite="ture"todir="${tomcat.home}/webapps"/>
    </target>
    <targetname="default">
       <echomessage="java version = ${ant.java.version}"/>
       <echomessage="ant version = ${ant.version}"/>
       <echomessage="---------请输入参数--------"/>
       <echomessage="输入 [ant help] 获取助信息"/>
    </target>
    <targetname="help">
       <echomessage="1. ant compile( ant c) (编译所有Java源文件)"/>
       <echomessage="2. ant native (编译所有Java源文件)"/>
       <echomessage="3. ant clean (清除classes目录下的所有类文件)"/>
       <echomessage="4. ant run.tomcat (启动Tomcat服务器)"/>
       <echomessage="5. ant close.tomcat (关闭Tomcat服务器)"/>
       <echomessage="6. ant doc (生成API文档 )"/>
    </target>
</project>

 
#log4j.rootLogger = INFO, A1
log4j.rootLogger=DEBUG,A1
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{yyyy-MM-ddhh:mm:ss}:%p%t%c-%m%n

web.xml配置文件
 
<?xml version="1.0" encoding="GBK"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
 
    <description>ajax聊天室</description>
 
    <context-param>
       <param-name>configFile</param-name>
       <param-value>WEB-INF/chatroom-config.xml</param-value>
    </context-param>
    <listener>
       <listener-class>
           org.jvk.chatroom.listener.InitChatroom
       </listener-class>
    </listener>
    <!-- 字符过滤 -->
    <filter>
       <filter-name>CharacterEncoding</filter-name>
       <filter-class>
           org.jvk.chatroom.filter.CharacterEncoding
       </filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>
    </filter>
    <filter-mapping>
       <filter-name>CharacterEncoding</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>
 
    <filter>
       <filter-name>chatroomFilter</filter-name>
       <filter-class>
           org.jvk.chatroom.filter.ChatroomFilter
       </filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>
    </filter>
    <filter-mapping>
       <filter-name>chatroomFilter</filter-name>
       <url-pattern>/pages/chatroom.jsp</url-pattern>
    </filter-mapping>
 
    <!-- login -->
    <servlet>
       <description>login</description>
       <display-name>login</display-name>
       <servlet-name>login</servlet-name>
       <servlet-class>org.jvk.chatroom.servlet.Login</servlet-class>
    </servlet>
 
    <servlet-mapping>
       <servlet-name>login</servlet-name>
       <url-pattern>/login</url-pattern>
    </servlet-mapping>
 
    <servlet>
       <display-name>getMessage</display-name>
       <servlet-name>getMessage</servlet-name>
       <servlet-class>
           org.jvk.chatroom.servlet.GetMessage
       </servlet-class>
    </servlet>
 
    <servlet-mapping>
       <servlet-name>getMessage</servlet-name>
       <url-pattern>/getMessage</url-pattern>
    </servlet-mapping>
 
    <servlet>
       <servlet-name>getUserList</servlet-name>
       <servlet-class>
           org.jvk.chatroom.servlet.GetUserList
       </servlet-class>
    </servlet>
 
    <servlet-mapping>
       <servlet-name>getUserList</servlet-name>
       <url-pattern>/getUserList</url-pattern>
    </servlet-mapping>
 
    <servlet>
       <servlet-name>sendMessage</servlet-name>
       <servlet-class>
           org.jvk.chatroom.servlet.SendMessage
       </servlet-class>
    </servlet>
 
    <servlet-mapping>
       <servlet-name>sendMessage</servlet-name>
       <url-pattern>/sendMessage</url-pattern>
    </servlet-mapping>
 
    <jsp-config>
       <jsp-property-group>
           <url-pattern>*.jsp</url-pattern>
           <page-encoding>UTF-8</page-encoding>
           <include-prelude>/pages/inc/taglib.inc</include-prelude>
       </jsp-property-group>
    </jsp-config>
 
</web-app>
 

相关文章推荐

AJAX_JSP 聊天室1_0 源码

  • 2007年07月27日 13:51
  • 12KB
  • 下载

HTML5 WebSocket+Tomcat实现真●Web版即时聊天室(单人+多人)

心累,下班回到宿舍,花了一个多小时的时间打开网页。。。真实醉了,对于干IT的人来说,没有网络或者网络卡到爆,真实比割了JJ还难受。首先是LZ的电脑被别人中了木马,是mysql的漏洞,在图书馆连了公共W...
  • qqHJQS
  • qqHJQS
  • 2015年10月11日 07:47
  • 9150

jsp,ajax实现的聊天室

  • 2012年02月20日 10:51
  • 2.45MB
  • 下载

jsp聊天室AJAX

  • 2009年04月25日 23:56
  • 232KB
  • 下载

html5 websocket 新版协议聊天室 服务端(python版)

网上找了很多代码都是旧版协议的,研究了很久终于弄清楚了 现在发个用新版协议写的服务端代码出来(这个代码是从网上旧版协议改过来的) 最要就是握手协议和发送接受字符的方式变了 # incoding=u...

Ajax+jsp做的聊天室

  • 2009年11月03日 11:54
  • 4KB
  • 下载

jsp+ajax实现简单聊天室

  • 2008年03月23日 17:50
  • 13KB
  • 下载

JSP在线聊天室——实验五之软件运行界面2

管理界面                 输入用户名搜索聊天记录         输入:ninarming---à搜索         返回的是管理界面         修改公告栏 ...

基于ajax+jsp的最经典的聊天室

  • 2008年04月27日 14:17
  • 6KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ajax聊天室(JSP版)5
举报原因:
原因补充:

(最多只允许输入30个字)