JSP web chat code.

Source: Internet
Author: User

Create a login page

<% @ Page Language = "Java" pageencoding = "gb2312" %>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> User Logon </title>
</Head>

<Body>
<Form action = "chart. jsp" method = "Post">
Account <input type = "text" name = "username"/>
Password <input type = "password" name = "passwd"/>
<Input type = "Submit" value = "OK"/>
</Form>
</Body>
</Html>

 

Create a chat page named chart. jsp

<% @ Page contenttype = "text/html; charset = GBK" Import = "Java. util. *" %>

<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";

%>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> my JSP 'chart. js' starting page </title>

<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
<% Response. setintheader ("refresh", 10); %>
</Head>

<Body>
Welcome to the website Chat System
<%
/**
Purpose: To simulate a chat room Program
Test method: open three IE Windows on the local machine and go to the logon interface. Speak again.
The response may not be very fast. Wait for a while. I have tested it.
**/
Try {
// Obtain the user passed through the user login interface
String loginname = request. getparameter ("username ");
// Obtain the user's sessionid
String usersessionid = (string) Session. getattribute ("usersessionid ");
// Retrieve the user account based on sessionid
String name = (string) Session. getattribute (usersessionid + "user ");
// If loginnmae! = NULL indicates the first time the user logs in
If (loginname! = NULL)
{// Create a sessionid for the first login and use the current system time as the sessionid
Usersessionid = system. currenttimemillis () + "";
Session. setattribute ("usersessionid", usersessionid );
// The storage account is in the session
Session. setattribute (usersessionid + "user", loginname );

}
// Obtain the user's speech
String word = request. getparameter ("yourwords ");
// Speaks from history of all users in the Application
String chart = (string) application. getattribute ("chart ");
// When the speech reaches a certain length, it will be cleared to avoid depletion of memory
If (chart! = NULL & chart. Length ()> 10000) application. setattribute ("chart ","");
If (name! = NULL & word! = NULL ){
// Assemble the statement that the user speaks on the screen:
String userword = new date () + "username: [" + name + "]:" + word;
// Save the user's speech to the history
Chart + = userword + "/R/N ";
Application. setattribute ("chart", chart );
}

%>
<Textarea rows = "15" Cols = "40" name = "output" style = "width: 100%">
<% IF (name! = NULL) Out. Print (Chart );
%>
</Textarea>
<% IF (name! = NULL) Out. Print (name + "say"); %>
<Form action = "" method = "Post">
<Input type = "text" size = "45" name = "yourwords">
<Input type = "hidden" name = "chart"> <br>
<Input type = "Submit" value = "send"/>
</Form>
</Body>
</Html>

<%} Catch (exception e ){
E. printstacktrace ();
} %>

 

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.