主要用到技术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("
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10395457/viewspace-967203/ ,如需转载,请注明出处,否则将追究法律责任。