码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet笔记10--Session

时间:2017-02-07 16:34:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:getattr   attr   data   fse   编程   print   新建   vax   tps   

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

标签:getattr   attr   data   fse   编程   print   新建   vax   tps   

原文地址:http://www.cnblogs.com/qjjazry/p/6374333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!