如何将新标头传递给sendRedirect [英] How to pass new header to sendRedirect

查看:99
本文介绍了如何将新标头传递给sendRedirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这应该很容易.我有一个应用程序,我想做的就是拥有一个表单页面(index.jsp),该页面调用一个Servlet(CheckInfo.java),该servlet设置一个新的标头(myHeader)并将用户重定向到另一个页面(redirect.jsp).所有这些文件都在同一服务器上.index.jsp可以很好地发送请求,而CheckInfo可以处理和重定向,但是myHeader不会显示在redirect.jsp上.我已经阅读了几篇有关response.sendRedirect的文章,其中发送了302且不传递标头,我应该使用RequestDispatcher,但似乎无济于事.没有办法将标头从servlet发送到jsp吗?这是servlet代码:

I feel like this should be easy. I have an app where all I am trying to do is have a form page (index.jsp) that calls a servlet (CheckInfo.java) which sets a new header (myHeader) and redirects the user to another page (redirect.jsp). All of these files are on the same server. The index.jsp is sending the request just fine and CheckInfo is processing and redirecting, but myHeader is not showing up on redirect.jsp. I've read several posts talking about response.sendRedirect sends a 302 which doesn't pass headers and that I should use RequestDispatcher, but nothing seems to work. Is there no way to send headers from a servlet to a jsp? Here is the servlet code:

response.setHeader("myHeader", "hey there");
response.sendRedirect("redirect.jsp");

我也尝试过这个:

response.setHeader("myHeader", "hey there");
RequestDispatcher view = request.getRequestDispatcher("redirect.jsp");
view.forward(request, response);

我在redirect.jsp中有这个

And I have this in redirect.jsp:

System.out.println(request.getHeader("myHeader"));

这不会打印任何内容.

如果对我的问题的回答是否定的...那么一旦我回到jsp,我将解决一种设置标头的方法.我的反向代理正在寻找特定的标头,以确定是否执行某项操作.显然,我在redirect.jsp上尝试了response.addHeader(),但是页面当时已经加载了,所以让我感到很笨.

If the answer to my question is no... then I would settle for a way to set the header once I got back to the jsp. My reverse proxy is looking for a specific header to determine whether or not to perform an action. Obviously I tried response.addHeader() on redirect.jsp, but the page has already loaded at that point so that just made me feel dumb.

推荐答案

response.setHeader("myHeader", "hey there");
response.sendRedirect("redirect.jsp");

您正在将其添加为响应标头,它是302响应.浏览器看到302响应后,只会查找 Location 标头,然后向该位置发送新请求.响应中的自定义标头保持不变,而您希望这些自定义响应标头包含在未发送的请求中(到新的重定向位置).

You are adding it as response header and it is 302 response. Browser on seeing a 302 response will just look for Location header and fire a new request to this location. Custom headers in the response are untouched whereas you are expecting these custom response headers to be included in the request (to new redirect location) which is not being sent.

解决方案:-
1.您可以使用请求分配器并转发请求,而不是外部重定向.您需要在此处使用请求属性.

2.您可以使用ajax请求调用提交表单,该请求可能类似于jquery并手动处理响应(对于302响应),但不建议您使用此方法,因为它不是一种更干净直观的方法.只是提及,以便您知道还有其他方法可以实现这一目标.

Solution:-
1. you can use request dispatcher and forward the request instead of external redirect. And you need to use request attributes here.

2. you can call submit form using an ajax request may be jquery like and handle the response manually(for 302 response) but would not suggest you to use this approach as it is not a cleaner and intuitive approach. Just mentioning so that you know there are other ways to achieve this.

这篇关于如何将新标头传递给sendRedirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆