用ServletContext做一个简单的聊天室

这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问。由此可见ServletContext比cookie和session的作用范围要大【session,cookie,和ServletContext是Servlet中比较重要的几个对象】

用ServletContext做一个简单的聊天室的思路是:把客户欧阳凤所输入的内容,保存在ServletContext中,在客户泗溪中用ServletContext的方法,获取客户欧阳凤所输入的内容。同时,在客户欧阳凤的界面得到客户泗溪所输入的内容。

两个客户的运行时的界面如图所示:

用ServletContext做一个简单的聊天室

用ServletContext做一个简单的聊天室

//ClientA的代码如下:

 package com.qls2;

 import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ClientA extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String clientB = request.getParameter("sixi");

String clientB = request.getParameter("sixi");
if (clientB!=null) {
byte[] b = clientB.getBytes("iso-8859-1");
clientB = new String(b, "utf-8");
}

out.println(clientB);

         out.println("<h1>欧阳凤</h1>");
out.println("<form action='/servletContext/CreateServletContext2' method='post'>");
out.println("请输入:<input type='text' name='ouyangfeng'/>");
out.println("<input type='submit' value='发送'/>");
out.println("</form>");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
this.doGet(request, response);
} }

//CreateServletContext2 的代码如下:

 package com.qls2;
/**
* 用ServletContext制作一个聊天室。
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CreateServletContext2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取ClientA输入的内容:
String clientA = request.getParameter("ouyangfeng"); this.getServletConfig().getServletContext().setAttribute("secreteriat", clientA);
request.getRequestDispatcher("/ReadServletContext2").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
this.doGet(request, response);
} }

//ReadServletContext2的代码如下:

 package com.qls2;

 import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ReadServletContext2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String str=(String) this.getServletContext().getAttribute("secreteriat"); out.println(str);
out.println("<h1>泗溪</h1>");
out.println("<form action='/servletContext/ClientA' method='post'>");
out.println("请输入:<input type='text' name='sixi'/>");
out.println("<input type='submit' value='发送'/>");
out.println("</form>"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
this.doGet(request, response);
} }
上一篇:Mysql5.5升级到5.7后MySQLdb不能正常使用的问题解决


下一篇:1391 检查是否存在有效路径 DFS