Issue
I've been working with Java servlets and noticed a peculiar problem in the following code:
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);
In particular, this line:
request.setAttribute("header", header);
I set both the String identifier and the variable name as the same. When I call this variable in my .jsp file via ${header}
, I get the following output:
{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}
However, when I change the String identifier from "header"
to "head"
and call ${head}
in the .jsp page, I get the expected output.
My question is, what is going on here?
Solution
JSP defines some implicit objects, to allow the JSP access to the same information that the Servlet code has.
These implicit objects are assigned at page scope, so they supersede any request attribute of the same name.
Solutions:
Don't use a request attribute name that matches an implicit object name.
Qualify access with
requestScope
, i.e. use${requestScope.header}
in your JSP.
I'd recommend option 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
Answered By - Andreas