从包含 # 的 url 在 java servlet 之间重定向

发布于 2024-12-27 15:12:27 字数 1062 浏览 0 评论 0原文

嘿,

也许标题不是最好的选择,但我真的不知道如何更好地描述问题。

问题是,当您将浏览器指向包含 # 的网址

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

并且出于某种原因(即存在业务逻辑)您想要重定向到其他页面时,

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

#dogeatdog 将添加到新网址。

我在开发 wicket 应用程序时发现了这种行为,但刚才我用简单的纯 java servlet 测试了它。有人可以向我解释一下吗?

这是代码,以防万一我做错了什么:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2025-01-03 15:12:27

哈希片段 (#a_hash_fragment) 永远不会离开浏览器,它们不是 HTTP 请求的一部分。

在这种情况下,Web 服务器获取的是 GET /test/elsem/1234,并使用重定向 3xx 代码和新网址 /test/els/1234 进行响应,其中您的浏览器会选择并附加#dogeatdog。现在有意义吗?

更新:感谢 Zack,这里有一份 W3C 文档,准确地解释了它(应该)如何工作:
http://www.w3.org/Protocols /HTTP/Fragment/draft-bos-http-redirect-00.txt

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

未央 2025-01-03 15:12:27

来自 sendRedirect Javadoc

使用指定的方法向客户端发送临时重定向响应
重定向位置 URL。该方法可以接受相对URL;这
servlet容器必须将相对URL转换为绝对URL
在将响应发送给客户端之前。如果位置是相对的
如果没有前导“/”,容器会将其解释为相对于
当前请求 URI。如果位置与前导“/”相关
容器将其解释为相对于 servlet 容器根。

由于在 Javadoc 中重复使用 "relative",我怀疑新 URL 正在使用旧 URL 中可以使用的内容,然后从那里构建......

在我所了解的简短内容中阅读,如果可能的话应该使用转发而不是重定向。

请参阅,了解正向重定向的详细说明。

有关将请求转发到 Servlet 或 JSP 的简单示例,请参阅

当然,通过转发,原始 URL 将保持不变,因此这可能不是您要查找的内容...

编辑
通过来自米兰的信息,我发现了一些关于URL片段的更多信息(“#”后面的东西——直到与米兰通信时我才知道这是他们的官方名称)。

还有另一篇 SOF 文章提供了一些与此相关的好信息,并且可能是最好的答案:URL Fragment and 302 redirects

我有“+1”米兰在这方面提供了良好的指导......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文