ITPub博客

首页 > 应用开发 > IT综合 > chat room

chat room

原创 IT综合 作者:nonamedemo 时间:2007-08-31 09:57:48 0 删除 编辑

主要用到技术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/,如需转载,请注明出处,否则将追究法律责任。

下一篇: JSP for shopping box
请登录后发表评论 登录
全部评论
  • 博文量
    31
  • 访问量
    11501