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

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

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

登录界面:

  

聊天界面:

  

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

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

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

web.xml的配置如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  7.   <display-name></display-name>     
  8.   <welcome-file-list> 
  9.     <welcome-file>index.htm</welcome-file> 
  10.   </welcome-file-list> 
  11.    
  12.   <servlet> 
  13.     <servlet-name>test</servlet-name> 
  14.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class
  15.     <load-on-startup>1</load-on-startup> 
  16.     </servlet> 
  17.  
  18.     <servlet-mapping> 
  19.     <servlet-name>test</servlet-name> 
  20.     <url-pattern>*.htm</url-pattern> 
  21.     </servlet-mapping> 
  22.  
  23.     <listener> 
  24.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class
  25.     </listener> 
  26.      
  27.     <filter> 
  28.         <filter-name>CharacterEncodingFilter</filter-name> 
  29.         <filter-class>com.nono.Filter.CharacterEncodingFilter</filter-class
  30.         <init-param> 
  31.             <param-name>encoding</param-name> 
  32.             <param-value>UTF-8</param-value> 
  33.         </init-param> 
  34.     </filter> 
  35.      
  36.     <filter>   
  37.         <filter-name>SecurityServlet</filter-name>   
  38.         <filter-class>com.nono.SecurityServlet</filter-class>   
  39.     </filter>   
  40.     <filter-mapping>   
  41.         <filter-name>SecurityServlet</filter-name>   
  42.         <url-pattern>*.htm</url-pattern>   
  43.     </filter-mapping> 
  44.      
  45.  
  46.     <!--  
  47.         使用Spring中的过滤器解决在请求和应答中的中文乱码问题  
  48.     <filter> 
  49.         <filter-name>characterEncodingFilter</filter-name> 
  50.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class
  51.         <init-param> 
  52.             <param-name>encoding</param-name> 
  53.             <param-value>utf-8</param-value> 
  54.         </init-param> 
  55.         <init-param> 
  56.                 强制转换编码(request和response均适用)  
  57.             <param-name>ForceEncoding</param-name> 
  58.             <param-value>true</param-value> 
  59.         </init-param> 
  60.     </filter> 
  61.     --> 
  62.     
  63.     <context-param> 
  64.         <param-name> 
  65.         contextConfigLocation 
  66.         </param-name> 
  67.         <param-value> 
  68.         /WEB-INF/test-servlet.xml 
  69.         </param-value> 
  70.     </context-param> 
  71. </web-app> 

 

conteConfigLocation的配置为:

 

  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
  4.  xmlns:context="http://www.springframework.org/schema/context" 
  5.  xmlns:aop="http://www.springframework.org/schema/aop" 
  6.  xmlns:tx="http://www.springframework.org/schema/tx" 
  7.  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  8.  xmlns:task="http://www.springframework.org/schema/task" 
  9.  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  10.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  11.       http://www.springframework.org/schema/context 
  12.       http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  13.       http://www.springframework.org/schema/tx 
  14.       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
  15.       http://www.springframework.org/schema/aop 
  16.       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
  17.       http://www.springframework.org/schema/mvc 
  18.       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
  19.       http://www.springframework.org/schema/task 
  20.       http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 
  21.      
  22.  
  23.     <context:annotation-config> </context:annotation-config> 
  24.     <context:component-scan base-package="com.nono" > </context:component-scan> 
  25.      
  26.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  27.         <property name="suffix"
  28.             <value>.jsp</value> 
  29.         </property> 
  30.     </bean> 
  31. </beans> 

整个项目的结构为一个主路由, 四个po层,  两个过滤器:

  

 

界面的用户列表和用户内容列表用了ajax刷新, 感觉不错的说:

 

  1. <!-- 
  2.     修改pageEncoding为 utf-8 
  3.  --> 
  4. <%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%> 
  5. <% 
  6. String path = request.getContextPath(); 
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
  8. %> 
  9.  
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  11. <html> 
  12.   <head> 
  13.     <base href="<%=basePath%>"
  14.      
  15.     <title>login</title> 
  16.     <meta charset="utf-8"
  17.     <link href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"
  18.     <script src="http://cdn.bootcss.com/jquery/1.9.0/jquery.js"></script> 
  19.   </head> 
  20.   <style> 
  21.       html,body,.bg{ 
  22.           height:100%; 
  23.       } 
  24.       .bg{ 
  25.           background:url(imgs/bg.jpeg); 
  26.       } 
  27.   </style> 
  28.   <body> 
  29.         <div class="container-fuild bg"
  30.             <div class="row"
  31.                 <div class="col-sm-4"
  32.                     <div class="page-header"
  33.                         <h2> 
  34.                             list 
  35.                         </h2> 
  36.                         <ol id="list"
  37.                             <li>name—</li> 
  38.                             <li>name—</li> 
  39.                             <li>name—</li> 
  40.                             <li>name—</li> 
  41.                         </ol> 
  42.                     </div> 
  43.                 </div> 
  44.                 <div class="col-sm-8"
  45.                         <h2> 
  46.                             content 
  47.                         </h2> 
  48.                     <div id="con" class="page-header"
  49.                       <p> 
  50.                           <b>haha:</b> 
  51.                           <big> 
  52.                               say someting 
  53.                           </big> 
  54.                       </p> 
  55.                       <p> 
  56.                           <b>haha:</b> 
  57.                           <big> 
  58.                               say someting 
  59.                           </big> 
  60.                       </p> 
  61.                     </div> 
  62.                     <form> 
  63.                       <div class="form-group"
  64.                         <label for="text">enter text</label> 
  65.                         <input type="text" id="answer" class="form-control" id="text" placeholder="text"
  66.                       </div> 
  67.                       <button type="button" id="sb" class="btn btn-default">Submit</button> 
  68.                     </form> 
  69.                 </div> 
  70.             </div> 
  71.         </div> 
  72.         <script> 
  73.             $("#sb").click(function() { 
  74.                 $.post("chat.htm""content="+ $("#answer").val(), function(data) { 
  75.                     console.log(data); 
  76.                 }); 
  77.             }); 
  78.              
  79.             function Get(url , el, fn) { 
  80.                 this.post = function() { 
  81.                     $.post(url, function(data) { 
  82.                         data = JSON.parse(data); 
  83.                         var html = ""
  84.                         $.each(data,function(i, e) { 
  85.                             html += fn(i,e); 
  86.                         }); 
  87.                         $(el).html( html ); 
  88.                     }); 
  89.                 }; 
  90.             }; 
  91.              
  92.             (function() { 
  93.              
  94.                 var list = new Get("getList.htm""#list", function(i, e) { 
  95.                     return  "<li>" + e.name + "</li>"
  96.                 }); 
  97.                  
  98.                 var content =  new Get("getContent.htm""#con", function(i, e) { 
  99.                     return "<p><b>"+ e.name +" : </b><big>"+ e.content +"</big></p>"
  100.                 }); 
  101.                  
  102.                 setInterval(function() { 
  103.                     list.post(); 
  104.                     content.post(); 
  105.                 },1000); 
  106.                  
  107.             })(); 
  108.         </script> 
  109.   </body> 
  110. </html> 

 

权限控制的话我们可以用到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.         forint i= 0; i< list.size(); i++ ) { 
  121.             HashMap<String,String> 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过的也是乱码, 坑爹啊;

责任编辑:王雪燕 来源: 博客园
相关推荐

2021-11-16 14:25:38

JavaScript前端

2016-09-14 21:17:47

PythonAsyncio游戏

2016-09-22 21:12:14

2016-09-19 21:24:08

PythonAsyncio游戏

2019-06-04 08:00:00

机器人聊天机器人人工智能

2011-09-28 11:22:52

Hadoop

2013-02-22 09:28:45

MEAP软件移动应用开发HTML5

2013-06-07 09:57:21

iPaaSSaaS集成iPaaS解决方案

2012-05-21 10:19:31

Hadoop

2023-04-13 21:59:44

ChatGPT聊天机器人

2011-12-31 14:24:13

2014-06-10 15:07:19

Oracle数据库优化

2014-06-04 13:19:29

C++ndk安卓开发

2009-06-15 10:21:07

基于JBossMBean

2020-01-09 09:43:12

Signal聊天应用开源

2017-06-07 23:15:30

应用程序热补丁代码

2023-12-21 08:00:00

ChatGPT人工智能大型语言模型

2015-10-12 16:45:26

NodeWeb应用框架

2017-06-07 23:33:01

应用程序热补丁代码

2023-01-05 09:17:58

点赞
收藏

51CTO技术栈公众号