2013-10-24 59 views
0

任何人都可以指示我一個簡單的例子(代碼)顯示使用response.encodeURL()? 我所有的seaches(谷歌和計算器)的唯一供應我找顯示它如何執行嵌入在鏈路會話信息一些簡單的代碼encodeURL()encodeRedirectURL().encodeURL()示例?

之間的差異。

感謝, 傑夫

回答

1
+0

謝謝,拉米。那麼,說'encodeURL()'應該只在輸出鏈接時才使用(例如,在HTML頁面或JSP中)嗎? –

+0

不客氣。沒錯,encodeURL應該用於向用戶輸出鏈接的情況,並且如果cookie被關閉,則需要保持會話狀態。 – Rami

1

考慮這一個:

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(); 
    }