Servlet-Session

Posted acgame

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet-Session相关的知识,希望对你有一定的参考价值。

 

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 }

运行结果:

技术分享图片

 

技术分享图片

 

以上是关于Servlet-Session的主要内容,如果未能解决你的问题,请参考以下文章

servlet-session

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板