如何用javaweb来写在线聊天应用

写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了;

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站建设、外贸网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的武冈网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

登录界面:

  

聊天界面:

  

左侧是在线的用户列表, 右侧是聊天的内容, 内容的格式为 “作者 : 内容”;

点击button可以发布聊天信息;

使用的是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.  

     路由控制和服务放到了一起, 因为权限控制使用过滤器处理, 所以在路由里面我们就不用关心用户的消息, 只要处理业务逻辑就好了:

     
     
    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.             //用户名已经存在 
    82.             request.setAttribute("info", "用户名已经存在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过的也是乱码, 坑爹啊;

    分享题目:如何用javaweb来写在线聊天应用
    网站URL:http://www.shufengxianlan.com/qtweb/news24/513924.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

    猜你还喜欢下面的内容

    网站设计知识

    同城分类信息