zoukankan      html  css  js  c++  java
  • session 的工作原理以及使用细节和url编码

    /**********************************************模拟页面************************************************************/

    package session;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class SessionDemo1 extends HttpServlet {
        //session底层封装了cookie所以,如果,浏览器禁用了cookie那么sessionID就没法带过去了
        //解决的办法就是,通过超链接的形式将sessionId带过去,此时超链接的url地址要经过编码
        //这样就保证,即使你把浏览器的cookie禁用掉,我也能将sessionId带过去
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //模拟这种场景
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;chatset=UTF-8");
            PrintWriter out = response.getWriter();
            
            request.getSession();
            //对url进行编码
            String url1 = response.encodeURL("/day07/SessionDemo2");
            String url2 = response.encodeURL("/day07/SessionDemo3");
            out.print("<a href='"+url1+"'>购物</a>     ");
            out.print("<a href='"+url2+"'>结帐</a>");
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

    }
    /********************************************************购买页面***************************************************************/

    package session;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;


    public class SessionDemo2 extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;chatset=UTF-8");
            
            HttpSession session = request.getSession();
            session.setAttribute("good", "电视机");
            
        }

        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

    }
    /*******************************************************8结帐页面******************************************************************/

    package session;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class SessionDemo3 extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;chatset=UTF-8");
            PrintWriter out = response.getWriter();
            
            HttpSession session = request.getSession(false);
            String value = (String) session.getAttribute("good");
            out.print("您购买的商品是"+value);
            
        }

        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

    }

  • 相关阅读:
    python处理孤立的异常点
    使用redis实现程序或者服务的高可用
    redis报错: redis.exceptions.ResponseError: value is not an integer or out of range
    angular6 使用信息提示框toast
    浏览器中模仿跨域请求
    python aes_cbc加密
    openresty钉钉免密登陆
    openresty 钉钉签名计算
    ansible服务部署
    tornado 文件上传
  • 原文地址:https://www.cnblogs.com/siashan/p/3916438.html
Copyright © 2011-2022 走看看