chat room

nonamedemo發表於2007-08-31

主要用到技術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("

MainServlet");
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("

");
out.println("");
out.println("");

out.println("");
out.println("");
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() {
}
}
=================================

Message servlet

================================

package chatroom;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class MessageServlet 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 Msg = request.getParameter("txtMsg");
String name = (String) request.getSession().getAttribute("loginName");
Message Msgobj = new Message();
Msgobj.setMsg(Msg);
Msgobj.setUserName(name);

ArrayList MsgList = null;
Object obj = this.getServletContext().getAttribute("MessageList");
if (obj == null) {
MsgList = new ArrayList();

} else {
MsgList = (ArrayList) obj;

}
MsgList.add(Msgobj);
this.getServletContext().setAttribute("MessageList", MsgList);
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() {
}
}
===================================

message bean entity class

===================================

package chatroom;

public class Message {
private String userName;
private String msg;
public Message() {
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getUserName() {
return userName;
}

public String getMsg() {
return msg;
}
}
===========================

index.html

==========================


login










姓名
密碼
   


[@more@]uo

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10395457/viewspace-967203/,如需轉載,請註明出處,否則將追究法律責任。

相關文章