Ajax Chat Room (JSP edition) 1__jsp

Source: Internet
Author: User
Tags ajax chat

Beginner JSP programming, generally like to do a message board, chat rooms, shopping sites or forums. Let me elaborate on how to implement a web-based, no refresh chat room.

Note: Refer to the struts version of the ajaxchat.

platform or technology:

Windows Xp,jsp2.0,servlet2.4,jstl,tomcat6,ajax,log4j,ant,eclipse, and so on.

idea:

1 page (information) Refresh

Because the HTTP protocol based Web determines that the server's connection to the client is not always there, the limitations of the web-style chat room are obvious. In a word: to realize the Web form of chat room, first of all to solve the problem of refreshing chat information.

There are two common practices: Before the advent of Ajax, using a hidden page (with an IFRAME tag) to send requests to the server, and to display the information returned by the server, after the advent of Ajax, the use of its asynchronous communication principles, in the background of the page "quietly" to the server to find requests. The way it is used here is the latter.

2 Functional Analysis

General steps for using a Web chat room: Go into a chat room, chat (chat to everyone, send a chat to a person), and exit the chat room.

Here is the simplest feature of the chat room that is described above. However, this is also a multiple-room multi-user chat room. An object-oriented approach to analysis, mainly involves three objects, namely: Chat information (message), chat room (room) (Room), users (user).

Chat information (message) should include: chat content, room, sender, recipient

Chat room (Room) should include: User (user list), chat information (chat information set)

Users (user) should include: User name, Room

After analyzing the above three basic objects, you can create the corresponding Java class. The following directly gives the three classes of code, specific analysis to let's.

PackageOrg.jvk.chatroom.bean;Publi C classMessage {//belong to a room (Room)PrivateString Room; Message contentPrivateString content; Send to (user)PrivateString to; By who sent, that belongs to the UserPrivateString from; Send TimePrivate LongDate; Public LongGetDate () { returnDate; } Public voidSetdate (LongDate) { This.     date = date; } PublicString getcontent () { returnContent; } Public voidSetContent (String content) { This.     Content = content; } PublicString Getfrom () { returnfrom; } Public voidSetfrom (String from) { This.     from = from; } PublicString Getroom () { returnRoom; } Public voidSetroom (String room) { This.     room = room; } PublicString Getto () { returnto; } Public voidSetto (String to) { This.     to = to; } }PackageOrg.jvk.chatroom.bean;ImportJava.util.ArrayList;ImportJava.util.List; Public classRoom {//InformationPrivatelist<message> messages =NewArraylist<message> (50) { Public BooleanAdd (Message message) {if( This. Size () > Maxmessage) This. Clear ();Booleanb =Super. Add (message);if(b) Messageupdatetime = System.currenttimemillis (); returnb       }     }; UserPrivatelist<user> users =NewArraylist<user> (50) { Public BooleanAdd (user user) {if( This. Size () > Maxuser) This. Clear ();Booleanb =Super. Add (user);if(b) Userlistupdatetime = System.currenttimemillis (); returnb } Public BooleanRemove (user user) {Booleanb =Super. remove (user);if(b) Userlistupdatetime = System.currenttimemillis (); returnb }     };Private LongUserlistupdatetime = System.currenttimemillis ();Private LongMessageupdatetime = Userlistupdatetime; Room namePrivateString name; Room NoticePrivateString notice; Room Keep maximum number of chat messagesPrivate intMaxmessage = 20; Maximum number of rooms reservedPrivate intMaxuser = 20;Private StaticList<user> nullusers =Newarraylist<user> (0);Private StaticArraylist<message> nullmessages =Newarraylist<message> (0); Public voidSetmaxmessage (intMaxmessage) { This.     Maxmessage = Maxmessage; } PublicString GetName () { returnname; } Public voidSetName (String name) { This.     name = name; } PublicString Getnotice () { returnNotice; } Public voidSetnotice (String notice) { This. notice = notice;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.