Java Servlet header 隐式 object
Java Servlet header implicit object
我一直在使用 Java servlet 并注意到以下代码中的一个特殊问题:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
String url = "/Display.jsp";
CSVFileOperations csvfo = new CSVFileOperations();
String header = csvfo.getHeaders().remove();
System.out.println(header);
request.setAttribute("header", header);
request.getServletContext().getRequestDispatcher(url).forward(request,
response);
特别是这一行:
request.setAttribute("header", header);
我把字符串标识符和变量名都设置成一样了。当我通过 ${header}
在我的 .jsp 文件中调用此变量时,我得到以下输出:
{accept-language=en-US, ua-cpu=AMD64,
cookie=JSESSIONID=1E0C2784352A46D6EFDE0F8A522F4, host=localhost:8080,
connection=Keep-Alive, cache-control=no-cache, accept-encoding=gzip,
deflate, accept=image/gif, image/jpeg, image/pjpeg, application/x-ms-
application, application/xaml+xml, application/x-ms-xbap, */*, user-
agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like
Gecko}
但是,当我将字符串标识符从 "header"
更改为 "head"
并在 .jsp 页面中调用 ${head}
时,我得到了预期的输出。
我的问题是,这是怎么回事?
JSP 定义了一些 implicit objects,以允许 JSP 访问 Servlet 代码具有的相同信息。
这些隐式对象在 页面 范围内分配,因此它们会取代任何同名的请求属性。
解决方案:
不要使用与隐式对象名称匹配的请求属性名称。
使用 requestScope
限定访问权限,即在您的 JSP.
中使用 ${requestScope.header}
我推荐选项 1。
The JSP expression language defines a set of implicit objects:
pageContext
: The context for the JSP page. Provides access to various objects including:
servletContext
: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
session
: The session object for the client. See Maintaining Client State.
request
: The request triggering the execution of the JSP page. See Getting Information from Requests.
response
: The response returned by the JSP page. See Constructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
param
: Maps a request parameter name to a single value
paramValues
: Maps a request parameter name to an array of values
header
: Maps a request header name to a single value
headerValues
: Maps a request header name to an array of values
cookie
: Maps a cookie name to a single cookie
initParam
: Maps a context initialization parameter name to a single value
Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope
: Maps page-scoped variable names to their values
requestScope
: Maps request-scoped variable names to their values
sessionScope
: Maps session-scoped variable names to their values
applicationScope
: Maps application-scoped variable names to their values
我一直在使用 Java servlet 并注意到以下代码中的一个特殊问题:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
String url = "/Display.jsp";
CSVFileOperations csvfo = new CSVFileOperations();
String header = csvfo.getHeaders().remove();
System.out.println(header);
request.setAttribute("header", header);
request.getServletContext().getRequestDispatcher(url).forward(request,
response);
特别是这一行:
request.setAttribute("header", header);
我把字符串标识符和变量名都设置成一样了。当我通过 ${header}
在我的 .jsp 文件中调用此变量时,我得到以下输出:
{accept-language=en-US, ua-cpu=AMD64,
cookie=JSESSIONID=1E0C2784352A46D6EFDE0F8A522F4, host=localhost:8080,
connection=Keep-Alive, cache-control=no-cache, accept-encoding=gzip,
deflate, accept=image/gif, image/jpeg, image/pjpeg, application/x-ms-
application, application/xaml+xml, application/x-ms-xbap, */*, user-
agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; rv:11.0) like
Gecko}
但是,当我将字符串标识符从 "header"
更改为 "head"
并在 .jsp 页面中调用 ${head}
时,我得到了预期的输出。
我的问题是,这是怎么回事?
JSP 定义了一些 implicit objects,以允许 JSP 访问 Servlet 代码具有的相同信息。
这些隐式对象在 页面 范围内分配,因此它们会取代任何同名的请求属性。
解决方案:
不要使用与隐式对象名称匹配的请求属性名称。
使用
中使用requestScope
限定访问权限,即在您的 JSP.${requestScope.header}
我推荐选项 1。
The JSP expression language defines a set of implicit objects:
pageContext
: The context for the JSP page. Provides access to various objects including:
servletContext
: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
session
: The session object for the client. See Maintaining Client State.
request
: The request triggering the execution of the JSP page. See Getting Information from Requests.
response
: The response returned by the JSP page. See Constructing Responses.In addition, several implicit objects are available that allow easy access to the following objects:
param
: Maps a request parameter name to a single value
paramValues
: Maps a request parameter name to an array of values
header
: Maps a request header name to a single value
headerValues
: Maps a request header name to an array of values
cookie
: Maps a cookie name to a single cookie
initParam
: Maps a context initialization parameter name to a single valueFinally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope
: Maps page-scoped variable names to their values
requestScope
: Maps request-scoped variable names to their values
sessionScope
: Maps session-scoped variable names to their values
applicationScope
: Maps application-scoped variable names to their values