这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问。由此可见ServletContext比cookie和session的作用范围要大【session,cookie,和ServletContext是Servlet中比较重要的几个对象】
用ServletContext做一个简单的聊天室的思路是:把客户欧阳凤所输入的内容,保存在ServletContext中,在客户泗溪中用ServletContext的方法,获取客户欧阳凤所输入的内容。同时,在客户欧阳凤的界面得到客户泗溪所输入的内容。
两个客户的运行时的界面如图所示:
//ClientA的代码如下:
1 package com.qls2; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.net.URLDecoder; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ClientA extends HttpServlet { 13 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 18 response.setContentType("text/html;charset=utf-8"); 19 PrintWriter out = response.getWriter(); 20 String clientB = request.getParameter("sixi"); 21 22
String clientB = request.getParameter("sixi");
if (clientB!=null) {
byte[] b = clientB.getBytes("iso-8859-1");
clientB = new String(b, "utf-8");
}
out.println(clientB);
23 out.println("<h1>欧阳凤</h1>"); 24 out.println("<form action='/servletContext/CreateServletContext2' method='post'>"); 25 out.println("请输入:<input type='text' name='ouyangfeng'/>"); 26 out.println("<input type='submit' value='发送'/>"); 27 out.println("</form>"); 28 } 29 30 31 public void doPost(HttpServletRequest request, HttpServletResponse response) 32 throws ServletException, IOException { 33 34 response.setContentType("text/html;charset=utf-8"); 35 PrintWriter out = response.getWriter(); 36 this.doGet(request, response); 37 } 38 39 }
//CreateServletContext2 的代码如下:
1 package com.qls2; 2 /** 3 * 用ServletContext制作一个聊天室。 4 */ 5 import java.io.IOException; 6 import java.io.PrintWriter; 7 import java.net.URLEncoder; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class CreateServletContext2 extends HttpServlet { 15 16 public void doGet(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 19 response.setContentType("text/html;charset=utf-8"); 20 PrintWriter out = response.getWriter(); 21 //获取ClientA输入的内容: 22 String clientA = request.getParameter("ouyangfeng"); 23 24 this.getServletConfig().getServletContext().setAttribute("secreteriat", clientA); 25 request.getRequestDispatcher("/ReadServletContext2").forward(request, response); 26 } 27 28 29 public void doPost(HttpServletRequest request, HttpServletResponse response) 30 throws ServletException, IOException { 31 32 response.setContentType("text/html;charset=utf-8"); 33 PrintWriter out = response.getWriter(); 34 this.doGet(request, response); 35 } 36 37 }
//ReadServletContext2的代码如下:
1 package com.qls2; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.net.URLDecoder; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 public class ReadServletContext2 extends HttpServlet { 13 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 18 response.setContentType("text/html;charset=utf-8"); 19 PrintWriter out = response.getWriter(); 20 String str=(String) this.getServletContext().getAttribute("secreteriat"); 21 22 out.println(str); 23 out.println("<h1>泗溪</h1>"); 24 out.println("<form action='/servletContext/ClientA' method='post'>"); 25 out.println("请输入:<input type='text' name='sixi'/>"); 26 out.println("<input type='submit' value='发送'/>"); 27 out.println("</form>"); 28 29 } 30 31 32 public void doPost(HttpServletRequest request, HttpServletResponse response) 33 throws ServletException, IOException { 34 35 response.setContentType("text/html;charset=utf-8"); 36 PrintWriter out = response.getWriter(); 37 this.doGet(request, response); 38 } 39 40 }