2

Can anyone direct me to a simple example (of code) showing the use of response.encodeURL()? All of my seaches (both google and stackoverflow) only supply the difference between encodeURL() and encodeRedirectURL().

I'm looking for some simple code showing how it would be implemented to embed session information in a link.

Thanks, Jeff

Jeff Levine
  • 2,083
  • 9
  • 30
  • 38

3 Answers3

2

Check those links:

Some background information:

http://www.coderanch.com/t/366059/Servlets/java/encodeURL-purpose-place

Code sample:

http://www.javadocexamples.com/javax/servlet/http/HttpServletResponse/encodeURL%28String%20url%29.html

Rami
  • 7,162
  • 1
  • 22
  • 19
  • Thanks, Rami. So, is it correct to say that `encodeURL()` should be used only when outputting links (for example, in an HTML page or JSP)? – Jeff Levine Oct 25 '13 at 00:03
  • You're welcome. That's right, encodeURL should be used in case a link is output to the user and you need to maintain the session state if cookies is turned off. – Rami Oct 25 '13 at 00:06
2

Consider this one:

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

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • Thanks for the clear example. Isn't HttpUtils.getRequestURL() a deprecate method? Is there any other replacement that we can use? – Dil. Apr 30 '18 at 08:03
  • 1
    Welcome. You can achieve it with `HttpServletRequest#getRequestURL()` – Farhan stands with Palestine Apr 30 '18 at 08:22
  • Thanks a lot.... Also there is a small question I had. When I refresh the page in second time, then above defined link automatically get clicked. What is the reason for that? – Dil. Apr 30 '18 at 10:12
1

When client disable cookies on browser, the container will not be able to manage session using cookies. At that time the Container will automatically use URL rewriting if the cookies are not available. But the Container still need to be told to "append jsessionid to the end of url". Therefore, response.encodeURL() is used to add jsessionid to the URL

code snip from HeadFirst E-Book

bad_coder
  • 11,289
  • 20
  • 44
  • 72