Can servlet attribute names contain a hyphen -
?
Because, I tried to retrieve the attributes from the request set in the doPost
in my servlet, but the result is not what I'm looking for.
In my servlet I have this :
String X_USER = request.getParameter("X-User");
request.setAttribute("X-User", X_USER);
String YYYY_YYYY = request.getParameter("Y_CODE");
request.setAttribute("Y-Code", YYYY_YYYY);
In my JSP where I want to show these attributes I do this:
<li>${X-User}</li>
<li>${Y-Code}</li>
The problem is that I'm getting 0 instead of the parameter value.
Your concrete problem is not the HTTP request parameter, but the EL variable name. All Java keywords and identifiers are illegal in EL variable names.
The
-
is a subtraction operator in Java (and also in EL). How should the EL processor know if you meant to use an attribute with the literal nameX-User
or the result of an integer subtraction of${User}
from${X}
? It's by specification interpreted as latter which also explains the numeric result of0
.Just use underscore or camelcase instead, like as in normal Java.
If you absolutely need to access a request attribute containing a hyphen in EL, then use the brace notationon the request scope map:
The same applies to request parameters, by the way, which you don't necessarily need to copy over into the request scope in the servlet:
See also:
You can use implicit object of jsp as
As you're setting attribute from servlet there is no need to get it via jstl.