简体   繁体   English

Java servlet - 从子页面转发/重定向

[英]Java servlet - Forwarding/Redirecting from subpage

I am working on a project where I have running servlets packaged in a war that listen for requests and populate a foreign div on a separate page. 我正在开发一个项目,我在战争中运行servlet,监听请求并在单独的页面上填充外部div。 An initial request is made to servlet (a) and if there is data to display, it renders that HTML. 向servlet(a)发出初始请求,如果有要显示的数据,则呈现该HTML。 If there is NO data, it passes a query string to another servlet to handle the request and then renders a page with options to choose. 如果没有数据,它会将查询字符串传递给另一个servlet以处理请求,然后呈现包含要选择的选项的页面。

I am running Tomcat 6 with Windows Server 2008. 我正在使用Windows Server 2008运行Tomcat 6。

But I run into two problems: 但我遇到两个问题:

  1. When I use redirect, I get no response from the servlet being redirected to. 当我使用重定向时,我没有得到重定向的servlet的响应。 I have some javascript alerts up that are never called by the (b) 我有一些javascript警报从未被(b)调用
    servlet. servlet的。 I AM using relative paths and confirm the link is correct in logs. 我使用相对路径并确认日志中的链接是正确的。
  2. When I copy that link in step 1 in a new window, I see the results. 当我在新窗口中复制步骤1中的链接时,我会看到结果。 Just not when it's embedded in another page that makes the request. 只是当它嵌入另一个发出请求的页面时。 Why would that be? 那为什么会这样? Is it possibly a limitation from the host page and not being able to render the response? 是否可能是主页的限制而无法呈现响应?
  3. When I use a forward, I see the servlet response, but then a new window opens. 当我使用forward时,我看到servlet响应,但随后会打开一个新窗口。 Thus taking the person away from the original page. 从而使人远离原始页面。 This would be great if the results render in the same page. 如果结果在同一页面中呈现,这将是很好的。

What's the best practice to assure that I can "redirect" from an initial servlet call to another servlet using the response object from that first servlet? 确保我可以使用第一个servlet的响应对象从初始servlet调用“重定向”到另一个servlet的最佳实践是什么?

A redirect returns a HTTP 302 response with the new URL in Location header which the client needs to deal with. 重定向返回HTTP 302响应,其中包含客户端需要处理的Location头中的新URL。 Basically, your JS code must check the response status code if it's 302 and then extract the Location header and then re-send a new request on it. 基本上,您的JS代码必须检查响应状态代码(如果它是302),然后提取Location标头,然后在其上重新发送新请求。 Repeat this until the response status code is 200. 重复此操作,直到响应状态代码为200。

That it works when pasting the URL in browser's address bar is because the browser already knows how to deal with 3nn responses properly. 在浏览器的地址栏中粘贴URL时它的作用是因为浏览器已经知道如何正确处理3nn响应。 If you open up the network traffic tracker in browser's webdeveloper toolset, then you'll see that a second GET request is been fired on the new URL. 如果您在浏览器的webdeveloper工具集中打开网络流量跟踪器,那么您将看到在新URL上触发了第二个GET请求。

Another way, if the servlets run in the same container, is to just use RequestDispatcher#forward() instead of HttpServletResponse#sendRedirect() . 另一种方法是,如果servlet在同一个容器中运行,只需使用RequestDispatcher#forward()而不是HttpServletResponse#sendRedirect()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM