zoukankan      html  css  js  c++  java
  • HttpSession

    模拟购物车页面页面

    package cn.soldier.session;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    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;
    
    /*
     * 模拟购物车页面页面
     * */
    @WebServlet("/sessionDemo1")
    public class SessionDemo1 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession();
            session.setAttribute("buy", "电视机");
    
            /*
             * encodeUrl
             * 
             * 为url构造JSESESSIONID,防止用户禁用cookie后,session失效。
             * 第一次访问时encodeUrl会构造JSESESSIONID,
             *  当判断到用户浏览器没有禁用cookie会关闭构造JSESESSIONID
             */
            String url1 = response.encodeUrl("ServletDemo/sessionDemo1");
            String url2 = response.encodeUrl("ServletDemo/sessionDemo2");
    
            out.write("<a href='" + url1 + "'>购买</a></br><a href='" + url2
                    + "'>结账</a>");
    
            // 使session失效
            /* session.invalidate(); */
    
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
    
        }
    }

    模拟结账页面

    package cn.soldier.session;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    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;
    import javax.servlet.http.HttpSession;
    
    /**
     * 模拟结账页面
     */
    @WebServlet("/sessionDemo2")
    public class SessionDemo2 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
    
            HttpSession session = request.getSession();
            String sessionId = session.getId();
            //设置cookie 是为了防止浏览器关闭用户丢失Session。
            Cookie cookie = new Cookie("JSESESSIONID",sessionId);
            cookie.setPath("day07");
            cookie.setMaxAge(60*30);
            response.addCookie(cookie);
            
            String product = (String) session.getAttribute("buy");
            out.write("您购买了" + product+"<br />");
            
            String url1 =response.encodeUrl("ServletDemo/sessionDemo1");
            String url2 =response.encodeUrl("ServletDemo/sessionDemo2");
            
            out.write("<a href='"+url1+"'>购买</a></br><a href='"+url2+"'>结账</a>");
    
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
    
        }
    }
  • 相关阅读:
    android之ConnectivityManager简介,网络连接状态
    SPOJ SUBLEX 7258. Lexicographical Substring Search
    poj 2417 Discrete Logging(A^x=B(mod c),普通baby_step)
    设计模式汇总
    微信公众平台预研小结
    Android开发之Handler的用法(源码分享)
    通过ccb(CocosBuilder)文件生成cocos2dx代码
    图像处理之错切变换
    combobox自己主动提示组件加入无选中项清空功能
    php 二维数组传递给 js 问题解决记录
  • 原文地址:https://www.cnblogs.com/lhy_2011/p/4044820.html
Copyright © 2011-2022 走看看