zoukankan      html  css  js  c++  java
  • 动手学servlet(四) cookie和session

    Cookie

      cookie是保存在客户端的一个“键值对”,用来存储用户的一些信息

       cookie的应用:

           -在电子商务会话中标识用户

           -对网站进行定制,比如你经常浏览哪些内容,就展示哪些页面给你

           -网站广告,比如百度联盟,你搜索了什么关键字,在各个网站就可以看到那个关键字的广告

      创建cookie

    package servletdemo;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class TestCookie
     */
    @WebServlet("/TestCookie")
    public class TestCookie extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public TestCookie() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
                    //构造函数第一个参数是key,第二个是value
            Cookie c=new Cookie("name", "henry");        
               //设置时间,最低级是秒,此处就是60秒乘以60,也就是一个小时
            c.setMaxAge(60*60);
                  //将创建的cookie添加到客户端
            response.addCookie(c);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    代码

      获取cookie

    package servletdemo;
    
    import java.io.IOException;
    import java.util.Iterator;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class TestGetCookie
     */
    @WebServlet("/TestGetCookie")
    public class TestGetCookie extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public TestGetCookie() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            Cookie [] cookies=request.getCookies();
            if(cookies!=null)
            {
                for(Cookie c:cookies)
                {
                    String name=c.getName();
                    String value=c.getValue();
                    System.out.println(name+":"+value);
                }
            }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    代码

      运行结果

    Session

      HTTP协议是无状态的,那么我们如何将对象数据持久化的保存呢,java有两个解决办法,一个是cookie,一个就是session,它是用来跟踪用户状态的一种机制,是针对浏览器和服务器的一对一关系

      Session接口HttpSession,通常我们会使用它,接口的实现由WEB容器来完成

      -可以从HttpServletRequest中获取HttpSession,request.getSession();返回一个HttpSession接口

      -将信息保存在HttpSession中,session.setAttribute("usersession",obj);

      -从HttpSession中获得信息,session.getAttribute("usersession");

      -使session失效,session.invalidate();

      创建session

    package servletdemo;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class SetSession
     */
    @WebServlet("/SetSession")
    public class SetSession extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public SetSession() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            HttpSession session=request.getSession();
            session.setAttribute("session_name", "session_value");
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    代码

      获得session

    package servletdemo;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class GetSession
     */
    @WebServlet("/GetSession")
    public class GetSession extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public GetSession() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            HttpSession session=request.getSession();
            String value=(String)session.getAttribute("session_name");
            System.out.println("value:"+value);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    代码

  • 相关阅读:
    无法访问计算机XXXX。错误是:库没有注册。
    Unix网卡重启命令
    教你如何进入有密码的 XP 系统
    巧妙恢复被破坏的Windows XP系统文件
    认识容灾备份 构筑一个避风港
    双机热备的实现模式 - 基于共享存储与纯软件方式
    玩大型游戏死机解决方法
    让windows xp开机跳过登陆界面
    在IE7中无法正常访问FTP的解决办法
    STL使用心得
  • 原文地址:https://www.cnblogs.com/SamFlynn/p/4480202.html
Copyright © 2011-2022 走看看