java request.getHeader 在 jsp 页面中返回空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14052217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:53:10  来源:igfitidea点击:

request.getHeader returning a null value in a jsp page

javahttp-headershttprequesthttpresponse

提问by Patan

I have written a filter to add some header values to Response object.

我编写了一个过滤器来向 Response 对象添加一些标头值。

The doFilter is setting some header value as shown below

doFilter 正在设置一些标头值,如下所示

public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain filterChain)
            throws IOException, ServletException 
            {   

        final HttpServletResponse response = (HttpServletResponse) res;
        final HttpServletRequest request = (HttpServletRequest) req;

        response.setHeader("X-FRAME-OPTIONS", "SAMEORIGIN");    

        filterChain.doFilter(req, res);     

    }

Filter mapping is "/*".

过滤器映射是"/*"

I have index.jsp page as welcome page and this is the only page in my application. I am trying to read the header value set in above method.

我有 index.jsp 页面作为欢迎页面,这是我的应用程序中唯一的页面。我正在尝试读取在上述方法中设置的标头值。

My index.jsp is

我的 index.jsp 是

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello world</title>
</head>
<body>

<%
    out.println("<br/><br/>XFRAME"+request.getHeader("X-FRAME-OPTIONS"));
%>
<br/>
</body>
</html>

I am getting Null output in the page. I could not get how I got null value if the value is set to "SAMEORIGIN".

我在页面中得到 Null 输出。如果值设置为“SAMEORIGIN”,我将无法获得空值。

XFRAMEnull 

can any one help on this.

任何人都可以对此提供帮助。

回答by Buhake Sindi

That's simple, you have added an attribute in the HttpServletResponseheaders and expect to read it from the HttpServletRequestheaders. That doesn't work that way.

这很简单,您已经在HttpServletResponse标题中添加了一个属性,并希望从HttpServletRequest标题中读取它。那样不行。

What you need to understand is the HTTP Protocol. The HttpServletRequestis the request coming from the client side to the server while the HttpServletResponseis the response of data and streams coming from the server and translated back to client message (rendered by the browser).

您需要了解的是 HTTP 协议。的HttpServletRequest是从客户端向服务器来的请求而HttpServletResponse为数据的响应,并从流服务器和翻译回客户端消息(由浏览器呈现的)来。

Each headers from either the request/response has information necessary for the server to know what it receives, how to translate the received data and what response to return. Hence why the HttpServletRequest.getHeaders()will almost never be identical to HttpServletResponse.getHeaders()as each of the request/response conforms to the HTTP request/response protocols.

请求/响应中的每个标头都包含服务器了解它接收到的内容、如何转换接收到的数据以及返回什么响应所必需的信息。因此,为什么HttpServletRequest.getHeaders()几乎永远不会完全相同,HttpServletResponse.getHeaders()因为每个请求/响应都符合 HTTP 请求/响应协议。

Also, you can never expect to populate a response and magically appear on the request.

此外,您永远不能期望填充响应并神奇地出现在请求中。

I hope this helps.

我希望这有帮助。