【问题标题】:encodeURL() example?编码URL()示例?
【发布时间】:2021-09-04 10:32:51
【问题描述】:

谁能指导我看一个显示response.encodeURL() 用法的简单示例(代码)? 我所有的搜索(谷歌和stackoverflow)只提供encodeURL()encodeRedirectURL().之间的区别

我正在寻找一些简单的代码来展示如何将会话信息嵌入到链接中。

谢谢, 杰夫

【问题讨论】:

    标签: url-rewriting session-cookies session-management


    【解决方案1】:

    【讨论】:

    • 谢谢,拉米。那么,encodeURL() 应该只在输出链接时使用(例如,在 HTML 页面或 JSP 中)是否正确?
    • 不客气。没错,应该使用encodeURL,以防向用户输出链接,并且如果cookie被关闭,您需要保持会话状态。
    【解决方案2】:

    考虑这个:

    public void doGet (HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
    
        // Get the session object. Create a new one if it doesn't exist.
        HttpSession session = req.getSession(true);
    
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
    
        out.println("<head><title> " + "SessionServlet Output " +
                    "</title></head><body>");
        out.println("<h1> SessionServlet Output </h1>");
    
        // Set up a session hit counter. "sessionservlet.counter" is just the
        // conventional way to create a key for the value to be stored in the
        // session object "dictionary".
        Integer ival = 
          (Integer) session.getAttribute("sessionservlet.counter");
        if (ival == null) {
          ival = new Integer(1);
        }
        else {
          ival = new Integer(ival.intValue() + 1);
        }
    
        // Save the counter value.
        session.setAttribute("sessionservlet.counter", ival);
    
        // Report the counter value. 
        out.println(" You have hit this page <b>" + 
                    ival + "</b> times.<p>");
    
        // This statement provides a target that the user can click
        // to activate URL rewriting. It is not done by default.
        out.println("Click <a href=" + 
                    res.encodeURL(HttpUtils.getRequestURL(req).toString()) + 
                    ">here</a>");
        out.println(" to ensure that session tracking is working even " +
                    "if cookies aren't supported.<br>");
        out.println("Note that by default URL rewriting is not enabled" +
                    " due to its large overhead.");
    
        // Report data from request.
        out.println("<h3>Request and Session Data</h3>");
        out.println("Session ID in Request: " +
                    req.getRequestedSessionId());
        out.println("<br>Session ID in Request is from a Cookie: " +
                    req.isRequestedSessionIdFromCookie());
        out.println("<br>Session ID in Request is from the URL: " +
                    req.isRequestedSessionIdFromURL());
        out.println("<br>Valid Session ID: " +
                    req.isRequestedSessionIdValid());
    
        // Report data from the session object.
        out.println("<h3>Session Data</h3>");
        out.println("New Session: " + session.isNew());
        out.println("<br> Session ID: " + session.getId());
        out.println("<br> Creation Time: " + new Date(session.getCreationTime()));
        out.println("<br>Last Accessed Time: " +
                    new Date(session.getLastAccessedTime()));
        out.println("</body>");
        out.close();
      }
    

    Reference

    【讨论】:

    • 感谢您提供的清晰示例。 HttpUtils.getRequestURL() 不是一个过时的方法吗?还有其他我们可以使用的替代品吗?
    • 欢迎。您可以通过HttpServletRequest#getRequestURL() 实现它
    • 非常感谢.... 还有一个小问题。当我第二次刷新页面时,上面定义的链接会自动被点击。这是什么原因?
    【解决方案3】:

    当客户端在浏览器上禁用 cookie 时,容器将无法使用 cookie 管理会话。 届时,如果 cookie 不可用,Container 将自动使用 URL 重写。 但是仍然需要告诉容器“将 jsessionid 附加到 url 的末尾”。 因此,response.encodeURL() 用于在 URL 中添加jsessionid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多