session是针对浏览器和服务器的一对一关系。
Test_Session.java:
1 package servlet_pack; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 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_Session 15 */ 16 @WebServlet("/Test_Session") 17 public class Test_Session extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public Test_Session() { 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 HttpSession session = request.getSession(); // 获得session实例,该接口的实现由容器实现 33 session.setAttribute("username", "admin"); // 共享数据 34 String username = (String)session.getAttribute("username"); // 获得数据 35 36 response.setContentType("text/html"); 37 PrintWriter out = response.getWriter(); 38 out.print("<!DOCTYPE html>"); 39 out.print("<html>"); 40 out.print("<head>"); 41 out.print("<title>A Servlet</title>"); 42 out.print("</head>"); 43 out.print("<body>"); 44 45 out.print(username); 46 47 out.print("</body>"); 48 out.print("</html>"); 49 } 50 51 /** 52 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 53 */ 54 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 55 // TODO Auto-generated method stub 56 doGet(request, response); 57 } 58 59 }
Test_Session_2.java:
1 package servlet_pack; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 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_Session_2 15 */ 16 @WebServlet("/Test_Session_2") 17 public class Test_Session_2 extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public Test_Session_2() { 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 HttpSession session = request.getSession(); // 获得session实例,该接口的实现由容器实现 33 String username = (String)session.getAttribute("username"); // 获得数据 34 35 response.setContentType("text/html"); 36 PrintWriter out = response.getWriter(); 37 out.print("<!DOCTYPE html>"); 38 out.print("<html>"); 39 out.print("<head>"); 40 out.print("<title>A Servlet</title>"); 41 out.print("</head>"); 42 out.print("<body>"); 43 44 out.print(username); 45 46 out.print("</body>"); 47 out.print("</html>"); 48 } 49 50 /** 51 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 52 */ 53 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 54 // TODO Auto-generated method stub 55 doGet(request, response); 56 } 57 58 }
运行结果: