gpt4 book ai didi

java - Servlet 请求阻塞

转载 作者:可可西里 更新时间:2023-11-01 16:31:16 25 4
gpt4 key购买 nike

我有一个过滤器,它扫描通过我的应用程序的所有请求。如果它请求特定的 url,我想阻止它。我可以检查这一点,如果条件匹配,我不会执行 chain.doFilter,但请求仍会移至黑页。我怎样才能阻止此请求继续前进并停留在当前页面 - 这意味着用户不应该看到他点击的屏幕有任何变化?

最佳答案

HTTP status code 204可能有效,但不能保证适用于所有浏览器:

204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

(强调我的)

或者,您可以尝试以下策略之一:

  1. 使用 HTTP Referer 字段让 servlet 返回 307(临时重定向)到上一页。这种方法可能会导致缓存和代理出现问题。
  2. 使用 ajax 从您的 servlet 加载内容。您将不得不用启动 ajax 调用的 JavaScript 调用来替换直接链接,以替换您的页面的一部分。当您以这种方式加载内容时,浏览器中的 URL 不会改变,这可能是合意的,也可能不是合意的。许多站点使用混合,包括 StackOverflow。任何应该是“永久链接”的内容都可以通过常规链接导航到,但每个页面的部分内容是通过 ajax 延迟获取的。

最后,告诉用户链接不可用有什么问题?让 servlet 返回 403(禁止)。

关于java - Servlet 请求阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com