日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何用javaweb來寫在線聊天應用

寫這個玩意兒就是想練練手, 用戶需要登陸才能在線聊天,不要依賴數(shù)據(jù)庫, 不需要數(shù)據(jù)庫的操作, 所有的數(shù)據(jù)都是保存在內存中, 如果服務器一旦重啟,數(shù)據(jù)就沒有了;

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站設計、網(wǎng)站建設、外貿網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的武岡網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

登錄界面:

  

聊天界面:

  

左側是在線的用戶列表, 右側是聊天的內容, 內容的格式為 “作者 : 內容”;

點擊button可以發(fā)布聊天信息;

使用的是spring搭建的框架,基于tomcat的服務器;

web.xml的配置如下:

 
 
  1.  
  2.     xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  6.        
  7.    
  8.     index.htm 
  9.    
  10.    
  11.    
  12.     test 
  13.     org.springframework.web.servlet.DispatcherServlet 
  14.     1 
  15.      
  16.  
  17.      
  18.     test 
  19.     *.htm 
  20.      
  21.  
  22.      
  23.         org.springframework.web.context.ContextLoaderListener 
  24.      
  25.      
  26.      
  27.         CharacterEncodingFilter 
  28.         com.nono.Filter.CharacterEncodingFilter 
  29.          
  30.             encoding 
  31.             UTF-8 
  32.          
  33.      
  34.      
  35.        
  36.         SecurityServlet   
  37.         com.nono.SecurityServlet   
  38.        
  39.        
  40.         SecurityServlet   
  41.         *.htm   
  42.      
  43.      
  44.  
  45.      
  46.     
  47.      
  48.          
  49.         contextConfigLocation 
  50.          
  51.          
  52.         /WEB-INF/test-servlet.xml 
  53.          
  54.      
  55.  

conteConfigLocation的配置為:

 
 
  1.  
  2.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  3.  xmlns:context="http://www.springframework.org/schema/context" 
  4.  xmlns:aop="http://www.springframework.org/schema/aop" 
  5.  xmlns:tx="http://www.springframework.org/schema/tx" 
  6.  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  7.  xmlns:task="http://www.springframework.org/schema/task" 
  8.  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  9.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  10.       http://www.springframework.org/schema/context 
  11.       http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  12.       http://www.springframework.org/schema/tx 
  13.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  14.       http://www.springframework.org/schema/aop 
  15.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
  16.       http://www.springframework.org/schema/mvc 
  17.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
  18.       http://www.springframework.org/schema/task 
  19.       http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 
  20.      
  21.  
  22.       
  23.       
  24.      
  25.      
  26.          
  27.             .jsp 
  28.          
  29.      
  30.  

整個項目的結構為一個主路由, 四個po層,  兩個過濾器:

  

界面的用戶列表和用戶內容列表用了ajax刷新, 感覺不錯的說:

 
 
  1.  
  2. <%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%> 
  3. <% 
  4. String path = request.getContextPath(); 
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  6. %> 
  7.  
  8.  
  9.  
  10.    
  11.     "> 
  12.      
  13.     login 
  14.      
  15.      
  16.      
  17.    
  18.    
  19.    
  20.          
  21.              
  22.                  
  23.                      
  24.                         

     

  25.                             list 
  26.                          
  27.                          
  28.                             
  29. name?
  30.  
  31.                             
  32. name?
  33.  
  34.                             
  35. name?
  36.  
  37.                             
  38. name?
  39.  
  40.                         
 
  •                     
  •  
  •                 
  •  
  •                  
  •                         

     

  •                             content 
  •                          
  •                      
  •                       

     

  •                           haha: 
  •                            
  •                               say someting 
  •                            
  •                       

     
  •                       

     

  •                           haha: 
  •                            
  •                               say someting 
  •                            
  •                       

     
  •                     
  •  
  •                     
     
  •                        
  •                         enter text 
  •                          
  •                       
  •  
  •                       Submit 
  •                      
  •                 
  •  
  •             
  •  
  •         
  •  
  •          
  •    
  •  
  • 權限控制的話我們可以用到fileter:

     
     
    1. package com.nono; 
    2.  
    3. import java.io.IOException; 
    4. import javax.servlet.Filter; 
    5. import javax.servlet.FilterChain; 
    6. import javax.servlet.FilterConfig; 
    7. import javax.servlet.ServletException; 
    8. import javax.servlet.ServletRequest; 
    9. import javax.servlet.ServletResponse; 
    10. import javax.servlet.http.HttpServlet; 
    11. import javax.servlet.http.HttpServletRequest; 
    12. import javax.servlet.http.HttpServletResponse; 
    13. import javax.servlet.http.HttpSession; 
    14.  
    15. import com.nono.po.User; 
    16.  
    17. public class SecurityServlet extends HttpServlet implements Filter { 
    18.     private static final long serialVersionUID = 1L; 
    19.  
    20.     public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { 
    21.            HttpServletRequest request=(HttpServletRequest)arg0;    
    22.            HttpServletResponse response  =(HttpServletResponse) arg1;     
    23.            HttpSession session = request.getSession(); 
    24.            User user = (User) session.getAttribute("user"); 
    25.            String url=request.getRequestURI();    
    26.            //如果用戶信息不是空的, 或者要訪問的是登陸的界面(get,post的方式包含了login字符串); 
    27.            if( user!=null  || url.indexOf("login")>-1 ) { 
    28.                arg2.doFilter(arg0, arg1);    
    29.                return; 
    30.            }else{ 
    31.                //余下的全跳到登陸界面 
    32.                response.sendRedirect(request.getContextPath() + "/login.htm"); 
    33.                return; 
    34.            } 
    35.     } 
    36.     public void init(FilterConfig arg0) throws ServletException { 
    37.     } 
    38.  

     路由控制和服務放到了一起, 因為權限控制使用過濾器處理, 所以在路由里面我們就不用關心用戶的消息, 只要處理業(yè)務邏輯就好了:

     
     
    1. package com.nono.Controller; 
    2.  
    3. import java.util.ArrayList; 
    4. import java.util.HashMap; 
    5. import java.util.Vector; 
    6.  
    7. import javax.jms.Session; 
    8. import javax.print.DocFlavor.STRING; 
    9. import javax.print.attribute.HashAttributeSet; 
    10. import javax.servlet.http.HttpServletRequest; 
    11. import javax.servlet.http.HttpServletResponse; 
    12. import javax.servlet.http.HttpSession; 
    13.  
    14. import net.sf.json.JSONArray; 
    15.  
    16. import org.omg.CORBA.PUBLIC_MEMBER; 
    17. import org.springframework.beans.factory.annotation.Autowired; 
    18. import org.springframework.stereotype.Controller; 
    19. import org.springframework.web.bind.annotation.RequestMapping; 
    20. import org.springframework.web.bind.annotation.RequestMethod; 
    21. import org.springframework.web.bind.annotation.ResponseBody; 
    22.  
    23. import com.nono.po.Content; 
    24. import com.nono.po.Contents; 
    25. import com.nono.po.User; 
    26. import com.nono.po.Users; 
    27.  
    28. @Controller 
    29. public class MainController { 
    30.     //用戶和用戶組; 
    31.     @Autowired 
    32.     Users users; 
    33.      
    34.     @Autowired 
    35.     Contents contents; 
    36.      
    37.     @RequestMapping(value="login", method=RequestMethod.GET) 
    38.     public String login (HttpServletRequest request) { 
    39.         return "login"; 
    40.     } 
    41.  
    42.      
    43.     @RequestMapping(value="login", method=RequestMethod.POST) 
    44.     public String loginPOST ( HttpServletRequest request, HttpServletResponse response ) { 
    45.          
    46.         String string = "login"; 
    47.         String name = (String) request.getParameter("name"); 
    48.         Boolean flag = true; 
    49.         //如果名字不是空的話; 
    50.         if( !name.equals("") ) { 
    51.             Vector vector = users.getList(); 
    52.             for(int i=0; i< vector.size(); i++) { 
    53.                 User user = (User) vector.elementAt(i); 
    54.                 if( user.getName().equals( name ) ) { 
    55.                     flag = false; 
    56.                 }; 
    57.             }; 
    58.         }; 
    59.          
    60.         //用戶名不存在 
    61.         if( flag ) { 
    62.             User user = new User(); 
    63.             user.setName( name ); 
    64.             HttpSession session = request.getSession(true); 
    65.             //設置Session的過期時間為10分鐘 
    66.             session.setMaxInactiveInterval(600); 
    67.             //設置seesion中的用戶信息; 
    68.             session.setAttribute("user", user); 
    69.             //添加用戶; 
    70.             users.addUser( user ); 
    71.              
    72.             //加入的提示; 
    73.             Content content = new Content(); 
    74.             content.setName( name ); 
    75.             content.setContent( "enter the chat room!" ); 
    76.             contents.addContent( content  ); 
    77.              
    78.             string = "chat"; 
    79.             return string; 
    80.         }else{ 
    81.             //用戶名已經(jīng)存在 
    82.             request.setAttribute("info", "用戶名已經(jīng)存在1"); 
    83.             string = "login"; 
    84.             return string; 
    85.         } 
    86.     } 
    87.      
    88.     @RequestMapping(value="chat", method=RequestMethod.GET) 
    89.     public String main (HttpServletRequest request) { 
    90.         String string = "chat"; 
    91.         return string; 
    92.     } 
    93.      
    94.     @RequestMapping(value="chat", method=RequestMethod.POST) 
    95.     @ResponseBody 
    96.     public String chat(HttpServletRequest request) { 
    97.         String string = (String) request.getParameter("content"); 
    98.         HttpSession session = request.getSession(); 
    99.         //設置seesion中的用戶信息; 
    100.         User user = (User) session.getAttribute("user"); 
    101.         String name = user.getName(); 
    102.         Content content = new Content(); 
    103.         content.setName( name ); 
    104.         content.setContent( string ); 
    105.         contents.addContent( content  ); 
    106.         return "true"; 
    107.     } 
    108.      
    109.     @RequestMapping(value="getList", method=RequestMethod.POST, produces = "text/html;charset=UTF-8") 
    110.     @ResponseBody 
    111.     public String getList( HttpServletRequest request) { 
    112.         return JSONArray.fromObject( users.getList() ).toString(); 
    113.     } 
    114.      
    115.     @RequestMapping(value="getContent", method=RequestMethod.POST, produces = "text/html;charset=UTF-8") 
    116.     @ResponseBody 
    117.     public String getArrayList() { 
    118.         ArrayList list = (ArrayList) contents.getContents(); 
    119.         ArrayList result = new ArrayList(); 
    120.         for( int i= 0; i< list.size(); i++ ) { 
    121.             HashMap hashMap = new HashMap(); 
    122.             hashMap.put("name", ((Content)list.get(i)).getName()); 
    123.             hashMap.put("content", ((Content)list.get(i)).getContent()); 
    124.             result.add( hashMap ); 
    125.         }; 
    126.         return JSONArray.fromObject( result ).toString(); 
    127.     } 
    128.      

    有哪位大神告訴我為什么中文各種亂碼, 在界面中的utf-8也設置, @ResponseBody的也設置了, 還是亂碼, encodeURIComponent過的也是亂碼, 坑爹?。?/p>
    網(wǎng)頁題目:如何用javaweb來寫在線聊天應用
    當前URL:http://www.5511xx.com/article/cddogso.html