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("

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

转载于:http://blog.itpub.net/10395457/viewspace-967203/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值