用來的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" />