chat room
主要用到技術servlet
==================
control servlet
===============================
package chatroom;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ControlServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String actiontype = request.getParameter("action");
if (actiontype != null &&actiontype.equals("login")) {
String name = request.getParameter("txtName");
if (name == null || name.equals("")) {
name = "遊客" + Math.random() % 100;
}
HttpSession session = request.getSession(true);
session.setAttribute("loginName", name);
ArrayList namelist = null;
Object obj = this.getServletContext().getAttribute("namelist");
if (obj == null) {
namelist = new ArrayList();
} else {
namelist = (ArrayList) obj;
}
namelist.add(name);
this.getServletContext().setAttribute("nameList", namelist);
response.sendRedirect("mainservlet");
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
============================
main serlvet
===========================
package chatroom;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MainServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out.println("");
out.println("
out.println("");
//顯示當前使用者
ArrayList namelist = (ArrayList)this.getServletContext().getAttribute(
"nameList");
Iterator iter = namelist.iterator();
while (iter.hasNext()) {
Object item = (Object) iter.next();
out.println(item + " ");
}
out.println("
");
//顯示聊天室打出的話
ArrayList MsgList = null;
Object obj = this.getServletContext().getAttribute("MessageList");
if (obj != null) {
MsgList = (ArrayList) obj;
Iterator iters = MsgList.iterator();
while (iters.hasNext()) {
Message item = (Message) iters.next();
out.println(item.getUserName() + "說: " + item.getMsg()+"
" +
"
");
}
}
out.println("
[@more@]uo
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-967203/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 2008.1.3 Chat room? I've heard it but I am not into it.OOM
- 人工智慧Chat GPT聊天工具:Hello Ai Chat for Chat GPT人工智慧GPTAI
- [譯] Room ? CoroutinesOOM
- 聊天chat封裝封裝
- Leetcode 252. Meeting Room 253. Meeting Room IILeetCodeOOM
- HDU 4923 Room and MoorOOM
- 人生房間Life RoomOOM
- Room Database完全使用手冊OOMDatabase
- Room ORM 資料庫框架OOMORM資料庫框架
- The room sexy Samantha Jones of Sex and the CityOOM
- Ubuntu安裝Rocket.ChatUbuntu
- jin-chat 基於 EasySwoole 和 layim 的聊天系統 (重構 woann-chat)
- Leetcode 253: meeting roomLeetCodeOOM
- [譯]從 SQLite 逐步遷移到 RoomSQLiteOOM
- [譯] 從 SQLite 逐步遷移到 RoomSQLiteOOM
- HDU 4923 Room and Moor(瞎搞題)OOM
- 免費 Chat | 從 JavaScript 到 TypeScriptJavaScriptTypeScript
- Room & Kotlin 符號的處理OOMKotlin符號
- Android O 遷移測試:RoomAndroidOOM
- Android架構元件Room的使用Android架構元件OOM
- 使用Room持久庫儲存資料OOM
- 快速向 Google Chat 傳送訊息Go
- OpenAI Chat completion API 入門指南OpenAIAPI
- 讓你一場 Chat 學會 GitGit
- zphp,swoole開發chat聊天室PHP
- 原生js實現發簡訊~chatJS
- [CareerCup] 8.7 Chat Server 聊天伺服器Server伺服器
- LiveData + ViewModel + Room (Google 官文)+DemoLiveDataViewOOMGo
- 【譯】遷移到Room的7個步驟OOM
- Android Architecture Components 之 Room 篇AndroidOOM
- 深入探討 Room 2.4.0 的最新進展OOM
- Android Room2.0之@TypeConverters使用AndroidOOM
- Microsoft Copilot(以前稱為 Bing Chat)ROS
- The Chinese Room的過去、現在和未來OOM
- [譯] 關於 Room 的 7 點專業提示OOM
- Leetcode Meeting room問題系列 - 2LeetCodeOOM
- Leetcode Meeting room問題系列 - 1LeetCodeOOM
- Android Room 之儲存 Objects 中的 ListAndroidOOMObject