Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape JSP EL using JSTL tags (dot character)

Tags:

java

jsp

jstl

el

Some frameworks (Spring, Tomcat itself) add servlet request attributes that cannot be used within an EL expression by default. An example would be

javax.servlet.forward.context_path = /myWebapp

So, to get the value using JSTL I'd normally use

<c:out value="${javax.servlet.forward.context_path}" />

However that's not working because the EL parser expects javaxto be the key of object A and servlet to be a property of that object (and so on).

So my question is: How do I escape the dot character?

I've tried using

<c:out value="${javax\.servlet\.forward\.context_path}" />

but that's not working either and raises an error from the EL parser.

I know that when dealing with maps I can use something like

<c:out value="${aMap['key.from.map.with.dots']}" />

but thats not working with a first level object from the request, since I've also tried using

<c:out value="${['javax.servlet.forward.context_path']}" />

which is not working either.

Any ideas?

like image 901
Christian Seifert Avatar asked Jul 15 '11 09:07

Christian Seifert


1 Answers

if you know the scope of the attribute, then you can fetch it from the appropriate implicit object, e.g.

${requestScope['javax.servlet.forward.context_path']}

I'm not sure if there's an implicit object that checks all scopes in the way that {xxx} does, though.

like image 178
skaffman Avatar answered Sep 24 '22 08:09

skaffman