ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:95.61KB ,
资源ID:20726231      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/20726231.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(构建类似Google的聊天室Word下载.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

构建类似Google的聊天室Word下载.docx

1、19 this.colorSelected = colorSelected;20 21 22 publicString getMessage() 23 returnMessage;24 25 26 publicvoidsetMessage(String message) 27 Message = message;28 29 30 publicString getUserName() 31 returnuserName;32 33 34 publicvoidsetUserName(String userName) 35 this.userName = userName;36 37 38 publ

2、icLong getId() 39 returnId;40 41 42 publicvoidsetId(Long id) 43 Id = id;44 45 46 publicString getMsgTime() 47 DateFormat dateFormat =newSimpleDateFormat(yyyy/MM/dd HH:mm:ss);48 Date date =newDate();49 returndateFormat.format(date);50 51 52 publicvoidsetMsgTime(String msgTime) 53 MsgTime = msgTime;54

3、 55 After creating the POJO class, create the hibernate configuration file, from which the application will come to know that how to map the tables column from database to the class members, database name and so on. The hibernate configuration file name must be “hibernate.cfg.xml” and it should resi

4、de at the root of source code.Code of hibernate.cfg.xml:1 2 !DOCTYPE hibernate-configuration PUBLIC3 -/Hibernate/Hibernate Configuration DTD/EN4 5 6 7 session-factoryname=java:hibernate/SessionFactory8 propertyname=hibernate.connection.driver_class com.mysql.jdbc.Driver9 hibernate.connection.url jdb

5、c:mysql:/localhost/test10 hibernate.connection.username username11 hibernate.connection.password pwd12 hibernate.connection.pool_size 1013 show_sql true14 dialect org.hibernate.dialect.MySQLDialect15 hibernate.hbm2ddl.auto update16 17 18 19 To keep the code simple, normally I add different configura

6、tions for different classes in hibernate.What is the need of dialect in hibernate:Databases implement subtle differences in the SQL they use. Things such as data types for example vary across databases Or database specific functionality selecting the top n rows is different depending on the database

7、. The dialect abstracts this so you dont have to worry about it. In short the dialect property internally creates the highly optimized query for underlying database.Here also, as you can see, I have included file ChatMessage.hbm.xml file.Code of ChatMessage.hbm.xml:xmlversion=1.0DOCTYPE hibernate-ma

8、pping PUBLIC-/Hibernate/Hibernate Mapping DTD 3.0/ENhibernate-mappingdefault-lazy=falseclassname=com.G2.pojo.ChatMessagetable=chatmessageidname=idtype=longcolumn=Idgeneratorclass=identity/idMessagecolumnname=userNameMsgTime20 colorSelected21 22 23 24 UI Part of the application:For the UI, I have wri

9、tten lots of css classes, this is not possible to write complete code here. Please check the code from download link provided at the bottom of the article.UI of the chat window is created to give the feel of the Gmail chat application as shown in below image:Gmail like chat window with emoticonsHere

10、, find the javascript code, which is responsible to send the AJAX request to the servlet, and getting back the response code:script type=text/javascript2 varuEnteredName = prompt(Please Enter Your Name3 $(#chat_Header).html(uEnteredName);4 $(#msg).focus();6 functionopenEmot()7 8 var$ele = $(#emotico

11、ns9 varvisibility = $ele.css(display10 11 if(visibility =none)12 13 $ele.show();14 15 else16 $ele.hide();17 18 19 20 functionsmileyCode(iconCode)21 22 var$msgEle = $(23 $msgEle.val($msgEle.val() + iconCode);24 var$ele = $(25 $ele.hide();26 27 $msgEle.focus();30 functionsaveChats() 31 varuName = $().

12、html();32 if(uName =) 33 alert(Please enter your name 34 returnfalse;35 36 varmsg = $().val();37 /var oldMsg = $(#chat-area38 varcolorCode = $(inputname=nameColor:checked,#send-message-area39 .val();40 41 $.ajax( 42 type :POST,43 data :uName=+ uName +&msg=+ msg +colorCode=44 + colorCode,45 url :Chat

13、process.do46 error :function(xhr, ajaxOptions, thrownError) 47 alert(xhr.status);48 alert(thrownError);49 ,50 success :function(data) 51 $().html(data);52 $(#ChatAtBigScreen53 document .getElementById(chat-area).scrollTop = document .getElementById().scrollHeight;54 document .getElementById(ChatAtBi

14、gScreen56 );57 returnfalse;58 59 $(#msg).keyup(function(e) 60 61 if(e.keyCode = 13) 62 saveChats();63 $().val(64 65 );66 Utility class, responsible to save the chat in the database using hibernate:1 packagecom.G2.Model;3 importjava.util.ArrayList;4 importjava.util.Iterator;5 importjava.util.List;7 i

15、mportorg.hibernate.Query;8 importorg.hibernate.Session;9 importorg.hibernate.SessionFactory;10 importorg.hibernate.Transaction;11 importorg.hibernate.cfg.Configuration;12 13 importcom.G2.pojo.ChatMessage;14 15 publicclassDBManager 16 / Read Hibernate.cfg.xml17 staticConfiguration cf =newConfiguratio

16、n().configure();18 staticSessionFactory factory = cf.buildSessionFactory();20 publicstaticvoidsaveChat(ChatMessage chat) 22 Session session =null;23 try24 25 session = factory.openSession();26 Transaction tx = session.beginTransaction();27 28 session.save(chat);29 mit();30 31 catch(Exception e) 32 e

17、.printStackTrace();33 finally34 if(session !=null) 35 / I faced problem here, if below line is not written then data automatically gets deleted after insertion36 session.flush();37 session.close();38 39 42 publicstaticList getMessages() 43 List MessageList =newArrayList();44 Session session =null;45

18、 try46 session = factory.openSession();47 String SQL_QUERY =from ChatMessage c;48 Query query = session.createQuery(SQL_QUERY);49 Iterator it = query.iterate();50 while(it.hasNext() 51 ChatMessage c = it.next();52 MessageList.add(c);53 54 55 catch(Exception e) 56 e.printStackTrace();57 finally58 ses

19、sion.flush();59 session.close();60 61 62 returnMessageList;63 As shown in above code, to work with hibernate, our application will need to read the configuration file of hibernate by below line of code:1 staticConfiguration cf =newConfiguration().configure();Now, we will need to create the session o

20、bject from the SessionFactory class:1 staticSessionFactory factory = cf.buildSessionFactory();2 Session session = factory.openSession();Create the object of transaction :1 Transaction tx = session.beginTransaction();To save the object in the database use save() method of the transaction.To get the l

21、ist of chat messages from the database :1 String SQL_QUERY =2 Query query = session.createQuery(SQL_QUERY);3 IteratorHere object of org.hibernate.Query is created and using method iterate(), it gives all the records from the database as a object. (That is what ORM Object Relation Mapping)Servlet Cod

22、e:1 packagecom.G2.servlets;3 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.util.HashMap;6 importjava.util.List;7 8 importjavax.servlet.ServletException;9 importjavax.servlet.http.HttpServlet;10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;13 importcom.G2.Model.DBManager;14 importcom.G2.pojo.ChatMessage;15 16 publicclassChatProcessextendsHttpServlet 18 Override19 prot

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1