用来的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
boolean
ignore
=
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
;
else
if
(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"?>
<project
name=
"ajax
聊天室
"
default=
"default"
basedir=
"E:/jsp/chatroom"
>
<!--
设置路径属性
-->
<property
environment=
"env"
/>
<!--
<property file="build.properties" />
-->
<property
name=
"tomcat.home"
value=
"G:/GreenSoftware/tomcat-6.0.10"
/>
<property
name=
"source.root"
value=
"${basedir}/src"
/>
<property
name=
"class.root"
value=
"${source.root}/web/WEB-INF/classes"
/>
<property
name=
"web.lib"
value=
"${source.root}/web/WEB-INF/lib"
/>
<property
name=
"tomcat.lib"
value=
"${tomcat.home}/lib"
/>
<property
name=
"dist"
value=
"${basedir}/dist"
/>
<property
name=
"appName"
value=
"chatroom"
/>
<!--
设置类文件路径
-->
<path
id=
"classpath"
>
<!--
包括自己编写的类
-->
<pathelement
location=
"${class.root}"
/>
<!--
包括
Tomcat
里的
servlet-api.jar
和
jsp-api.jar -->
<fileset
dir=
"${tomcat.lib}"
>
<include
name=
"servlet-api.jar"
/>
<include
name=
"jsp-api.jar"
/>
</fileset>
<fileset
dir=
"${web.lib}"
>
<include
name=
"*.jar"
/>
</fileset>
</path>
<!--
清理文件
-->
<target
name=
"clean"
>
<delete
dir=
"${class.root}"
/>
<delete
dir=
"${dist}"
/>
</target>
<!--
复制文件到
classes
目录下
-->
<target
name=
"prepare"
>
<mkdir
dir=
"${dist}"
/>
<copy
todir=
"${class.root}"
>
<fileset
dir=
"${source.root}/java"
>
<exclude
name=
"**/*.java"
/>
</fileset>
</copy>
</target>
<!--
编译
Java
源文件
-->
<target
name=
"compile"
depends=
"prepare"
>
<javac
classpathref=
"classpath"
srcdir=
"${source.root}/java"
destdir=
"${class.root}"
/>
</target>
<target
name=
"c"
depends=
"compile"
/>
<!--
打包成
war
文件
-->
<target
name=
"war"
depends=
"compile"
>
<jar
jarfile=
"${dist}/${appName}.war"
basedir=
"${source.root}/web"
/>
</target>
<target
name=
"zip"
depends=
"war"
>
<zip
zipfile=
"${dist}/${appName}_bin_src.zip"
>
<fileset
dir=
"${dist}"
>
<include
name=
"${appName}.war"
/>
</fileset>
<fileset
dir=
"${basedir}"
>
<include
name=
"src/**"
/>
<include
name=
"build.xml"
/>
</fileset>
</zip>
</target>
<!--
生成
API
文档
-->
<target
name=
"doc"
>
<javadoc
packagenames=
"*"
sourcepath=
"${source.root}/java"
defaultexcludes=
"yes"
destdir=
"${dist}/doc"
author=
"true"
version=
"true"
use=
"true"
windowtitle=
"${ant.project.name}"
/>
</target>
<!--
启动
Tomcat
服务器
-->
<target
name=
"run.tomcat"
>
<exec
dir=
"${tomcat.home}/bin"
executable=
"cmd.exe"
>
<arg
line=
"/c startup.bat"
/>
</exec>
</target>
<!--
关闭
Tomcat
服务器
-->
<target
name=
"close.tomcat"
>
<exec
dir=
"${tomcat.home}/bin"
executable=
"cmd.exe"
>
<arg
line=
"/c shutdown.bat"
/>
</exec>
</target>
<!--
部署到
Tomcat
服务器
-->
<target
name=
"deploy"
depends=
"war"
>
<delete
dir=
"${tomcat.home}/webapps/${appName}"
/>
<copy
file=
"${dist}/${appName}.war"
overwrite=
"ture"
todir=
"${tomcat.home}/webapps"
/>
</target>
<target
name=
"default"
>
<echo
message=
"java version = ${ant.java.version}"
/>
<echo
message=
"ant version = ${ant.version}"
/>
<echo
message=
"---------
请输入参数
--------"
/>
<echo
message=
"
输入
[ant help]
获取助信息
"
/>
</target>
<target
name=
"help"
>
<echo
message=
"1. ant compile(
或
ant c) (
编译所有
Java
源文件
)"
/>
<echo
message=
"2. ant native (
编译所有
Java
源文件
)"
/>
<echo
message=
"3. ant clean (
清除
classes
目录下的所有类文件
)"
/>
<echo
message=
"4. ant run.tomcat (
启动
Tomcat
服务器
)"
/>
<echo
message=
"5. ant close.tomcat (
关闭
Tomcat
服务器
)"
/>
<echo
message=
"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-dd
hh
:
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
>