[英]request.getHeader returning a null value in a jsp page
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. 我有index.jsp页面作为欢迎页面,这是我的应用程序中唯一的页面。 I am trying to read the header value set in above method. 我试图读取上面方法中设置的标头值。
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. 我在页面中获得Null输出。 I could not get how I got null value if the value is set to "SAMEORIGIN". 如果值设置为“SAMEORIGIN”,我无法得到如何获得null值。
XFRAMEnull
can any one help on this. 任何人都可以帮忙。
That's simple, you have added an attribute in the HttpServletResponse
headers and expect to read it from the HttpServletRequest
headers. 这很简单,您在HttpServletResponse
标头中添加了一个属性,并希望从HttpServletRequest
标头中读取它。 That doesn't work that way. 那不行。
What you need to understand is the HTTP Protocol. 您需要了解的是HTTP协议。 The HttpServletRequest
is the request coming from the client side to the server while the HttpServletResponse
is the response of data and streams coming from the server and translated back to client message (rendered by the browser). 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. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.