Servlet笔记10--Session

Posted 拉夫德尔

tags:

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

Web编程中的Session:

 

 

  代码示例:

 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpSession;
10 
11 public class AccessMySelfSessionServlet extends HttpServlet {
12 
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     protected void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         
19         String ip = request.getRemoteAddr();
20         HttpSession session = request.getSession();
21         System.out.println(ip + "\'s session = " + session);
22         //向session范围中存储数据
23         session.setAttribute("username", "zhangsan");
24     }
25 }
 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpSession;
10 
11 public class GetDataFromSessionServlet extends HttpServlet {
12 
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     protected void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         HttpSession session = request.getSession();
19         //从session范围中读取数据
20         Object username = session.getAttribute("username");
21         System.out.println(username);
22     }
23     
24     
25 }
 1 package com.bjpowernode.javaweb.servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpSession;
10 
11 public class LogoutServlet extends HttpServlet {
12 
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     protected void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18         
19         //获取session对象,若没有获取到session对象,则新建session对象
20 //        HttpSession session = request.getSession();
21         
22         //获取session对象,若没有获取到session对象,则新建session对象
23 //        HttpSession session = request.getSession(true);
24         
25         //获取session对象,若没有获取到session对象,则返回null
26         HttpSession session = request.getSession(false);
27         if(session != null){
28             //销毁session
29             session.invalidate();
30         }
31     }
32     
33     
34 }

 

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

学习笔记:python3,代码片段(2017)

Servlet笔记6--Servlet程序改进

Servlet笔记11--补充

Servlet笔记8--乱码解决方案

Servlet笔记9--转发与重定向

JavaWeb 笔记总结