2

Hey,

Maybe the title is not the best choice, but I really don't know how to better describe the problem.

The thing is when you point your browser to url that contains #

http://anydomain.com/test/elsem/1234#dogeatdog

and for some reason (ie. there is a business logic) you want to redirect to other page

http://anydomain.com/test/els/1234

the #dogeatdog will be added to new url.

I found this behavior while developing wicket app, but just now I tested it with simple pure java servlet. Can someone explain it to me?

Here is the code just in case I'm doing something wrong:

private void process(HttpServletRequest req, HttpServletResponse res)
{
    res.setContentType("text/plain");
    try
    {
        HttpSession session = req.getSession();
        Object as = session.getAttribute("as");
        if (as == null)
        {
            log.info("redirecting");
            session.setAttribute("as", 1);
            res.sendRedirect("/test/");
        }
        else
        {
            log.info("writing");
            PrintWriter out = res.getWriter();
            out.write("after redirect "+as);
            out.flush();
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
kamiseq
  • 593
  • 4
  • 17

2 Answers2

4

Hash fragments (#a_hash_fragment) never leave the browser, they are not part of HTTP request.

What the web server gets in this case is GET /test/elsem/1234, and it responds with redirect 3xx code and the new url /test/els/1234, which your browser picks and appends #dogeatdog. Makes sense now?

UPDATE: Thanks to Zack, here's a W3C document that exactly explains how this (should) work: http://www.w3.org/Protocols/HTTP/Fragment/draft-bos-http-redirect-00.txt

milan
  • 11,872
  • 3
  • 42
  • 49
  • When I go from http://stackoverflow.com/faq#questions to http://stackoverflow.com/about, the hash fragment doesn't stay in the browser (at least when using Chrome). – Zack Macomber Jan 19 '12 at 12:55
  • Is there an "official" document that states what you said about hash fragments? I'm asking more for curiosity and learning sake... – Zack Macomber Jan 19 '12 at 12:57
  • who said the fragment always stays there? I said it happens when you get 3xx redirect response. try on a page that does redirect, just append any hash and see what happens. – milan Jan 19 '12 at 12:59
  • "Hash fragments (#a_hash_fragment) never leave the browser" - I was zeroing in on that particular statement... – Zack Macomber Jan 19 '12 at 13:03
  • well, you can find at w3c http://www.w3.org/DesignIssues/Fragment for example: "The HTTP engine cannot make any assumptions about it. The server is not even given it.". And you can also test it your self, just inspect wire transfer for few browsers. – milan Jan 19 '12 at 13:10
  • 2
    Ok - thanks milan - just wanted to find more info out on it. Found this article from w3.org that gives more details on it as well - http://www.w3.org/Protocols/HTTP/Fragment/draft-bos-http-redirect-00.txt - it might be good for you to include those references as it sheds further light on OP's question – Zack Macomber Jan 19 '12 at 13:17
  • funny as I thought that #name is always part of the url itself as you usually add it to hyperlink, anyway I need to read all documents you shared here – kamiseq Jan 19 '12 at 17:17
  • well, it is :) it's 'just' that it always stays on the client side – milan Jan 19 '12 at 17:26
0

From the sendRedirect Javadoc:

Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

Because of repetitive use of "relative" in the Javadoc, I suspect the new URL is using what it can from the old URL and then building from there...

In the brief amount of what I've read, forwarding should be used if possible instead of redirect.

See this for a good explanation of forward verses redirect.

See this for straight-forward examples of forwarding requests to Servlets or JSPs.

Of course, with forwarding, the original URL will remain intact so that may not be what you're looking for...

EDIT
With information from milan, I found some more information regarding URL fragments (the stuff after "#" - I didn't know that was their official name until corresponding with milan).

There's another SOF post that has some good information concerning this and possibly the best answer: URL Fragment and 302 redirects

I have "+1'd" milan for giving good direction on this...

Community
  • 1
  • 1
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104