ajax聊天室(JSP版)5__JSP

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.