Java class used, in addition to the above several, there are several left.
Two filter classes, one for filtering characters, and one for filtering users who are not logged in.
PackageOrg.jvk.chatroom.filter;ImportJava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.servlet.http.HttpSession;ImportOrg.jvk.chatroom.servlet.BaseServlet; Public classChatroomfilterImplementsFilter {Private Static FinalString login_page = "/chatroom";Private Static FinalString USER = Baseservlet. USER; Public voidDofilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, 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 voidInit (Filterconfig filterconfig)throwsServletexception {} Public voidDestroy () {}}
PackageOrg.jvk.chatroom.filter;ImportJava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;ImportJavax.servlet.ServletException;ImportJavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classCharacterencodingImplementsFilter {protectedString encoding =NULL;protectedFilterconfig Filterconfig =NULL;protected BooleanIgnore =true; Public voidDestroy () { This. encoding =NULL; This. Filterconfig =NULL; } Public voidDofilter (ServletRequest request, servletresponse response, Filterchain chain)throwsIOException, Servletexception {if(Ignore | | (request.getcharacterencoding () = =NULL) {String encoding = selectencoding (request);if(Encoding!=NULL) request.setcharacterencoding (encoding); Chain.dofilter (request, response); } Public voidInit (Filterconfig filterconfig)throwsservletexception { 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; }protectedString selectencoding (ServletRequest request) { return( This. encoding); A listener is used to initialize the chat room (call the Chatroommanager config method).PackageOrg.jvk.chatroom.listener;ImportJava.io.File;ImportJava.io.IOException;ImportJavax.servlet.ServletContextEvent;ImportJavax.servlet.ServletContextListener;ImportOrg.apache.commons.logging.Log;ImportOrg.apache.commons.logging.LogFactory;ImportOrg.jvk.chatroom.service.ChatroomManager; Public classInitchatroomImplementsServletcontextlistener {Private StaticLog log = Logfactory.getlog (initchatroom.class); Public voidContextinitialized (Servletcontextevent event) {String configfile = Event.getservletcontext (). Getinitparameter ("Co Nfigfile ");Try{chatroommanager.newinstance (). config (NewFile (Event.getservletcontext (). Getrealpath (ConfigFile))); }Catch(IOException e) {log. Error (e);Throw NewRuntimeException (E.getmessage ()); } } Public void Contextdestroyed (Servletcontextevent event) { }} at this point, all Java class code is gone. Below begins to write the client's JS code and interface HTML code. By the way, post the configuration file as well. <?xml version= "1.0" encoding= "GBK"?> <project name= "ajax chat Room" default= "Default" basedir= "E:/jsp/chatroom" ; <!--set path properties --> <property environment= "env"/> & nbsp; <!-- <property file= "build.properties"/> --> <property name= "Tomcat.home" value= "g:/greensoftware/tomcat-6.0.10"/> < Property Name= "Source.root" value= "${basedir}/src"/>