Test_Scope.java:
1 package servlet_pack; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletContext; 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 /** 14 * Servlet implementation class Test_Scope 15 */ 16 @WebServlet("/Test_Scope") 17 public class Test_Scope extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public Test_Scope() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 30 */ 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 // request 请求,请求结束作用域结束 33 request.setAttribute("request_name", "request_value"); 34 35 // session 会话,服务器断开就结束 36 HttpSession session = request.getSession(); 37 session.setAttribute("session_name", "session_value"); 38 39 // ServletContext 全局,所有用户共享 40 ServletContext context = this.getServletContext(); 41 context.setAttribute("servletcontext_name", "servletcontext_value"); 42 43 request.getRequestDispatcher("/Res_Servlet").forward(request, response); // 请求转发 44 45 } 46 47 /** 48 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 49 */ 50 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 51 // TODO Auto-generated method stub 52 doGet(request, response); 53 } 54 55 }
Res_Servlet.java:
1 package servlet_pack; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletContext; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 14 /** 15 * Servlet implementation class Res_Servlet 16 */ 17 @WebServlet("/Res_Servlet") 18 public class Res_Servlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 /** 22 * @see HttpServlet#HttpServlet() 23 */ 24 public Res_Servlet() { 25 super(); 26 // TODO Auto-generated constructor stub 27 } 28 29 /** 30 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 31 */ 32 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 String request_value = (String)request.getAttribute("request_name"); 34 35 HttpSession session = request.getSession(); 36 String session_value = (String)session.getAttribute("session_name"); 37 38 ServletContext context = this.getServletContext(); 39 String servletcontext_value = (String)context.getAttribute("servletcontext_name"); 40 41 response.setContentType("text/html"); 42 PrintWriter out = response.getWriter(); 43 out.print("<!DOCTYPE html>"); 44 out.print("<html>"); 45 out.print("<head>"); 46 out.print("<title>A Servlet</title>"); 47 out.print("</head>"); 48 out.print("<body>"); 49 50 out.println(request_value); 51 out.println(session_value); 52 out.println(servletcontext_value); 53 54 out.print("</body>"); 55 out.print("</html>"); 56 } 57 58 /** 59 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 60 */ 61 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 62 // TODO Auto-generated method stub 63 doGet(request, response); 64 } 65 66 }
运行结果: