java写一个web聊天工具_javaweb写的在线聊天应用
写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了;
登录界面:
聊天界面:
左侧是在线的用户列表, 右侧是聊天的内容, 内容的格式为 “作者 : 内容”;
点击button可以发布聊天信息;
使用的是spring搭建的框架,基于tomcat的服务器;
web.xml的配置如下:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
index.htm
test
org.springframework.web.servlet.DispatcherServlet
1
test
*.htm
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
com.nono.Filter.CharacterEncodingFilter
encoding
UTF-8
SecurityServlet
com.nono.SecurityServlet
SecurityServlet
*.htm
contextConfigLocation
/WEB-INF/test-servlet.xml
View Code
conteConfigLocation的配置为:
.jsp
View Code
整个项目的结构为一个主路由, 四个po层, 两个过滤器:
界面的用户列表和用户内容列表用了ajax刷新, 感觉不错的说:
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
login
height:100%;
}
.bg{
background:url(imgs/bg.jpeg);
}
list
- name
- name
- name
- name
content
haha:
say someting
haha:
say someting
enter text
Submit
$.post("chat.htm", "content="+ $("#answer").val(), function(data) {
console.log(data);
});
});functionGet(url , el, fn) {this.post = function() {
$.post(url,function(data) {
data=JSON.parse(data);var html = "";
$.each(data,function(i, e) {
html+=fn(i,e);
});
$(el).html( html );
});
};
};
(function() {var list = new Get("getList.htm", "#list", function(i, e) {return "
" + e.name + "";
});var content = new Get("getContent.htm", "#con", function(i, e) {return "
"+ e.name +" : "+ e.content +"
";
});
setInterval(function() {
list.post();
content.post();
},1000);
})();
View Code
权限控制的话我们可以用到fileter:
packagecom.nono;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.nono.po.User;public class SecurityServlet extends HttpServlet implementsFilter {private static final long serialVersionUID = 1L;public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throwsIOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse) arg1;
HttpSession session=request.getSession();
User user= (User) session.getAttribute("user");
String url=request.getRequestURI();//如果用户信息不是空的, 或者要访问的是登陆的界面(get,post的方式包含了login字符串);
if( user!=null || url.indexOf("login")>-1) {
arg2.doFilter(arg0, arg1);return;
}else{//余下的全跳到登陆界面
response.sendRedirect(request.getContextPath() + "/login.htm");return;
}
}public void init(FilterConfig arg0) throwsServletException {
}
}
View Code
路由控制和服务放到了一起, 因为权限控制使用过滤器处理, 所以在路由里面我们就不用关心用户的消息, 只要处理业务逻辑就好了:
packagecom.nono.Controller;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Vector;importjavax.jms.Session;importjavax.print.DocFlavor.STRING;importjavax.print.attribute.HashAttributeSet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importnet.sf.json.JSONArray;importorg.omg.CORBA.PUBLIC_MEMBER;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.ResponseBody;importcom.nono.po.Content;importcom.nono.po.Contents;importcom.nono.po.User;importcom.nono.po.Users;
@Controllerpublic classMainController {//用户和用户组;
@Autowired
Users users;
@Autowired
Contents contents;
@RequestMapping(value="login", method=RequestMethod.GET)publicString login (HttpServletRequest request) {return "login";
}
@RequestMapping(value="login", method=RequestMethod.POST)publicString loginPOST ( HttpServletRequest request, HttpServletResponse response ) {
String string= "login";
String name= (String) request.getParameter("name");
Boolean flag= true;//如果名字不是空的话;
if( !name.equals("") ) {
Vector vector=users.getList();for(int i=0; i< vector.size(); i++) {
User user=(User) vector.elementAt(i);if( user.getName().equals( name ) ) {
flag= false;
};
};
};//用户名不存在
if( flag ) {
User user= newUser();
user.setName( name );
HttpSession session= request.getSession(true);//设置Session的过期时间为10分钟
session.setMaxInactiveInterval(600);//设置seesion中的用户信息;
session.setAttribute("user", user);//添加用户;
users.addUser( user );//加入的提示;
Content content = newContent();
content.setName( name );
content.setContent("enter the chat room!");
contents.addContent( content );
string= "chat";returnstring;
}else{//用户名已经存在
request.setAttribute("info", "用户名已经存在1");
string= "login";returnstring;
}
}
@RequestMapping(value="chat", method=RequestMethod.GET)publicString main (HttpServletRequest request) {
String string= "chat";returnstring;
}
@RequestMapping(value="chat", method=RequestMethod.POST)
@ResponseBodypublicString chat(HttpServletRequest request) {
String string= (String) request.getParameter("content");
HttpSession session=request.getSession();//设置seesion中的用户信息;
User user = (User) session.getAttribute("user");
String name=user.getName();
Content content= newContent();
content.setName( name );
content.setContent( string );
contents.addContent( content );return "true";
}
@RequestMapping(value="getList", method=RequestMethod.POST, produces = "text/html;charset=UTF-8")
@ResponseBodypublicString getList( HttpServletRequest request) {returnJSONArray.fromObject( users.getList() ).toString();
}
@RequestMapping(value="getContent", method=RequestMethod.POST, produces = "text/html;charset=UTF-8")
@ResponseBodypublicString getArrayList() {
ArrayList list=(ArrayList) contents.getContents();
ArrayList result= newArrayList();for( int i= 0; i< list.size(); i++) {
HashMap hashMap = newHashMap();
hashMap.put("name", ((Content)list.get(i)).getName());
hashMap.put("content", ((Content)list.get(i)).getContent());
result.add( hashMap );
};returnJSONArray.fromObject( result ).toString();
}
}
View Code
有哪位大神告诉我为什么中文各种乱码, 在界面中的utf-8也设置, @ResponseBody的也设置了, 还是乱码, encodeURIComponent过的也是乱码, 坑爹啊;
java写一个web聊天工具_javaweb写的在线聊天应用相关推荐
- java qq聊天界面_【附源码】用Java写了一个类QQ界面聊天小项目,可在线聊天!...
原标题:[附源码]用Java写了一个类QQ界面聊天小项目,可在线聊天! 目录: 1.功能实现 2.模块划分 3.使用到知识 4.部分代码实现 5.运行例图 1.功能实现 1.修改功能(密码.昵称.个性 ...
- api怎么写_使用Node.js原生API写一个web服务器
Node.js是JavaScript基础上发展起来的语言,所以前端开发者应该天生就会一点.一般我们会用它来做CLI工具或者Web服务器,做Web服务器也有很多成熟的框架,比如Express和Koa.但 ...
- 从 1 到完美,用 node 写一个命令行工具
从 1 到完美,用 node 写一个命令行工具 1. package.json 中的 bin 字段 现在,不管是前端项目还是 node 项目,一般都会用 npm 做包管理工具,而 package.js ...
- 用Python写一个纪念日计算工具
用python写一个纪念日计算工具 前言 主要内容 日期计算函数 GUI布局 用pyinstaller打包生成.exe文件 打包后的.exe文件预览 运行结果 最后提前祝马英俊生日快乐吧~ 前言 纪念 ...
- 怎么画图自动生成HTML,用canvas写一个简易画图工具
本文将为您描述用canvas写一个简易画图工具,具体操作方法: Document style="border:1px solid #000000;">您的浏览器不支持canv ...
- 你写一个web网页小游戏
写一个 web 网页小游戏需要以下几个步骤: 选择一种编程语言,常用的有 HTML.CSS.JavaScript 和 Python. 使用编辑器创建一个 HTML 文件,这个文件将是你的网页的基础. ...
- 用JavaScript写一个正则表达式测试工具
用JavaScript写一个正则表达式测试工具 <!DOCTYPE html> <html><head><meta charset="utf-8&q ...
- 自己写一个Web服务器(1)
自己写一个Web服务器(1) 2016-04-13 分类:操作系统.编程开发.首页精华1人评论 分享到:更多15 本文由码农网 – 王坚原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿 ...
- 如何用C语言写一个web服务器的基础功能
我们都知道,学一门语言,只是单独看了就不写的话是很容易出现眼高手低的,所以,今天摩杜云要给大家分享的内容,就是如何用C语言写一个web服务器的基础功能,希望大家看完有所收获. 服务器架构 目标架构 以 ...
最新文章
- debian/ubuntu 安装和使用perf
- 【C 语言】内存四区原理 ( 栈内存与堆内存对比示例 | 函数返回的堆内存指针 | 函数返回的栈内存指针 )
- 以太网与802.3,wifi与802.11的关系
- python中merge函数怎么用_Python Merge函数原理及用法解析
- @mapperscan注解_Spring的@Import注解详解
- Type(类型判断)
- 防仿百度图片背景色php,基于jQuery实现仿百度首页换肤背景图片切换代码_jquery...
- linux下声卡的安装
- 获取表情字符串的html,js:如何截取含有表情的字符串
- 聚合天气--ajax 通过城市名取数据
- 10692 XYM-入门之道
- 18. Django进阶:中间件
- Java代码增加回滚6_如何编写取消按钮的代码以撤消/回滚添加和更新正在进行的行4gl...
- DC-DC电路多种调制方式的介绍及对比
- JSPSSM316的网上水果生鲜销售商城系统java毕业设计成品源码介绍
- YeeCOM DTU 轮询采集
- python新版个人所得税代码_个人所得税计算代码
- 【seeprettyface.com】数据集:模特/明星人脸数据集
- 解密微信小程序用户敏感数据获取用户信息
- linux cp 中文文件名,linux下将中文文件名文件cp到windows目录下后文件名乱码问题的解决...
热门文章
- 小程序 mpvue 使用canvas绘制环形图表
- 怀化学院计算机科学专业排名,2019怀化学院专业排名
- powerdesigner 反向工程 oracle,PowerDesigner oracle 反向工程到cdm文件
- Ajax解析JSON文件
- Ms sqlserver闪退----“转载的知识”
- php代码质量怎么提高,如何提高PHP代码的质量?第一部分 自动化工具
- 凤凰网php,凤凰网某应用sql注入漏洞_MySQL
- 【C语言进阶深度学习记录】二 有符号与无符号
- Combobox报错:row[opts.textField].toLowerCase is not a function。
- 架构实战:(一)Redis采用主从架构的原因