• 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 }
  • 相关阅读:
    下面是C语言中两种if语句判断方式。请问哪种写法更好?为什么?
    c语言二级指针简单练习
    C++ Dlib库介绍和编译使用
    Win10怎么设置开机自动启动程序?
    C 可变参数
    Qt 进程守护程序(windows、linux)
    Leetcode 面试题 17.04. 消失的数字
    TCP的三次握手与四次挥手理解及面试题(很全面)
    studio visual 插件下载太慢 visual studio 下载慢的解决办法
    人脸检测(Python dlib)
  • 原文地址:https://www.cnblogs.com/qjjazry/p/6374333.html
Copyright © 2020-2023  润新知