Jsp is a popular dynamic web page language. This article briefly introduces how to use jsp to implement chat room programs.
Design Ideas for chat rooms
1. function and structure description
User Registration and logon. Non-registered access is supported.
When a user logs on, enter the user name and password. If this is the first time a user is used, the system will prompt you to create a new user. If you are prompted to register for a non-registered user, you can directly enter. After the verification is successful, enter the main chat page, which consists of three windows: chat information display, user list, and message sending. In the message sending section, enter the information and select related options to send the information. When you exit, information about the user list is automatically deleted.
1.1 display information
The server maintains a ChatMessage of the Vector type chat information data stored in the application. The single message is spliced using strings. The basic structure is as follows:
Speaker + Action + receiver + speech content,
Information is spliced by the sender.
To implement the private chat function, you can add a receiver data domain on the basis of this structure, place it at the beginning of the information structure, and separate it with a combination of special symbols such. When accepting the display, it is extracted first to determine whether the local display should be performed. The default value is all online users.
Of course, you can open a new window to only display private chat information, which is basically the same.
Set a variable in the user session to store the number of the last chat message when the user logs on. Local display information is displayed from this line. When the server clears the chat information, this variable is set to 0, and all subsequent information is displayed (Private Chat information is displayed as needed ). For ease of control, information is displayed in reverse order, that is, the last sent information is displayed at the top of the page, omitting the scrolling operation.
The scrolling function can be completed using javascript scripts.
1.2 message sending
Extract and splice information. It is mainly composed of forms, including the Speaker (hidden), Action, texture, select, check, text, and submit) and exit (button or href ). The getParameter () method of the request is used to extract the content of each data field and splice it into a single message. The receiver information is added based on whether the message is private or not.
1.3 user list
Displays the list of online users and their details. Auto refresh or manual refresh. User details are stored in a Hashtable-type userlist in the application. The keyword "nick" is used as the key. The benefit of Hashtable is that the user information can be quickly deleted and the operation is simple when the user exits. Convert it to Enumeration During display to Display User Information cyclically.
The user's detailed information is composed of various parts. You can extract each part before display. User information is added to Hashtable after the user logs in successfully.
1.4 User Registration
The user fills in the relevant information at the corresponding position in the Form. After extracting the information on the registration page, the user queries the information based on nick in the database. If nick already exists, the user is prompted to fill in nick again, on the contrary, the user information is directly inserted into the database and displayed on the page. The user selects to log on or leave.
The check here should have a lot of details. For example, the special characters cannot be used and the emial format is incorrect.
1.5 login verification
The user nick and password are extracted. If the user is a registered user, use nick to query the relevant information in the database for password comparison. After successful login, the other information is extracted and spliced, add nick to Hashtabel as the keyword. For non-registered users, add other default information and add Hashtable. Check whether there are duplicates in this case. Hashtable features can easily achieve this.
2. Specific design points
It mainly uses String-type variables, which can be directly displayed in Html.
Set the Meta attribute of the page to refresh.
Note the null check of the variable. Otherwise, unnecessary exceptions may occur during debugging.
This design does not use Javabean. The actual design should try to encapsulate important Code such as database access and user list maintenance into bean.
The kick function can be implemented as follows:
1. Record the last speech time for each user. Refresh the user list room to automatically compare the current time. If the time exceeds a certain period, kick is triggered and a prompt message is sent.
2. Set a counter to store the number of new information after the last speech. When it is greater than a certain number
Kick. In reality, this number can correspond to a fixed time (which can be obtained based on probability knowledge)
All source files are as follows:
Login. jsp completes user login input
Apply. jsp User Registration Information Input, modify user information
Applyok. jsp database operations during user registration
Getinfo. jsp: Get the list and details of the current online user.
Password verification and initialization during frame. jsp User Logon
Send. jsp send information
Select the profile picture when registering scface. jsp
Reiceve. jsp receives and displays chat Information
Exit. jsp user exit update online user
Database
Use jdbc: odbc bridge to connect to the database and create an odbc Data Source chat
Here, the database uses the common microsoft access
Tables in the database:
Chat (Nickname, Password, Face, Sex, QQ, Email)
The table contains attributes such as Nickname, Password, Face, Sex, QQ, and Email. All attributes are set to the text type.
The first two fields are required. Note the length of each attribute.
Deployment:
Because javabean is not used in the program, you can directly use it as a part of an existing project during deployment (copy all files to the corresponding folder ).
It can also be deployed as a new project. For more information, see related documents. Modify the sever. xml file in the conf folder under the root directory when using Apach Tomcat 4.1. This is not detailed here.
Development Environment:
Apach Tomcat 4.1 + Editplus 2
To change the environment, please refer to the relevant documentation to modify the relevant content.
This program is a learning exercise, and some functions are only designed but not actually implemented. All source files are provided. If you are interested, you can add new functions (send me a source code ). The design style of the interface refers to the popular design on the Internet. You can access a chat room to obtain the html code by yourself (most of them can only see the html code, and some design ideas can be analyzed ). Many features of jsp are not reflected here, such as security, handling of various exceptions, or failing to provide implementation code, due to time issues, if there is any improvement, it will be added and pasted.
Please kindly advise.
Beginners make progress together.
Me: JJ
Email: kensmail@yeah.net
The letter is always required.