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();
    }
}
2
  • out of curiosity - does the same thing happen when you do a forward instead of a redirect? Commented Jan 18, 2012 at 20:47
  • Following @Zack's lines, I would suggest you to try this using RequestDispatcher. Commented Jan 18, 2012 at 20:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

When I go from stackoverflow.com/faq#questions to stackoverflow.com/about, the hash fragment doesn't stay in the browser (at least when using Chrome).
Is there an "official" document that states what you said about hash fragments? I'm asking more for curiosity and learning sake...
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.
"Hash fragments (#a_hash_fragment) never leave the browser" - I was zeroing in on that particular statement...
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 - 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
|
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.