zoukankan      html  css  js  c++  java
  • url override and HttpSession implements session for real

    无论cookie有没有禁用,HttpSession都有效
    package com.test;
    
    
    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 java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet("/page1")
    public class Page1 extends HttpServlet {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
    
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<meta charset='UTF-8'>");
            out.println("<title>问卷调查</title>");
            out.println("</head>");
            out.println("<body>");
    
            String url = response.encodeURL("page2");
            out.println("<form action="" + url + "" method="get">");
            out.println("問題一:<input type='text' name='p1q1'><br>");
            out.println("問題二:<input type='text' name='p1q2'><br>");
            out.println("<input type='submit' name='page' value='提交'>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    }
    View Code
    package com.test;
    
    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;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet("/page2")
    public class Page2 extends HttpServlet {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            HttpSession session = request.getSession();
    
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<meta charset='UTF-8'>");
            out.println("<title>问卷调查</title>");
            out.println("</head>");
            out.println("<body>");
    
            String url = response.encodeURL("page3");
            out.println("<form action="" + url + "" method="get">");
            System.out.println("第一个sessionId: " + session.getId());
            session.setAttribute("p1q1", request.getParameter("p1q1"));
            session.setAttribute("p1q2", request.getParameter("p1q2"));
            out.println("問題三:<input type='text' name='p2q1'> <br>");
            out.println("<input type='submit' name='page' value='提交'>");
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    }
    View Code
    package com.test;
    
    
    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;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet("/page3")
    public class Page3 extends HttpServlet {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            HttpSession session = request.getSession();
    
            PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<meta charset='UTF-8'>");
            out.println("<title>问卷调查</title>");
            out.println("</head>");
            out.println("<body>");
    
            System.out.println("第二个sessionId: " + session.getId());
            out.println(session.getAttribute("p1q1") + "<br>");
            out.println(session.getAttribute("p1q2") + "<br>");
            out.println(request.getParameter("p2q1") + "<br>");
    
            out.println("</form>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    }
    View Code

    测试地址

      127.0.0.1:8080/page1

  • 相关阅读:
    样条之拉格朗日Lagrange(一元全区间)插值函数[转]
    样条之埃尔米特(Hermite)[转]
    安装Elasticsearch-head插件
    ElasticSearch常用命令
    mysql 导出表结构和表数据 mysqldump用法
    对Elastic集群内部配置TLS加密通信及身份验证
    Paxos算法与Zookeeper的工作原理
    Kafka重平衡机制
    Power Apps UI flows setup 安装失败
    使用Power Apps 创建门户应用
  • 原文地址:https://www.cnblogs.com/Mike_Chang/p/10054887.html
Copyright © 2011-2022 走看看