How can I print a servlet context attribute in EL?
Asked Answered
S

1

8

Is there a way I can get a attribute set in ServletContext in EL so that it ends up as a JavaScript variable?

I am setting it as

context.setAttribute("testing.port", "9000");

I tried retrieving it like

alert("port" +'${testing.port}');

I am just getting a blank.

Syzygy answered 30/11, 2011 at 4:0 Comment(0)
C
8

The problem is the period (.) in the key name. EL interprets the period as a call to an accessor method named getPort1 on whatever object testing references. Fetch the value from the appropriate implicit object:

${applicationScope['testing.port']}

or just use a different key:

${testingPort}

1Yes, this is a simplification of what really happens. It may also look for a predicate getter named isPort, or try Map#get("port").

Caesura answered 30/11, 2011 at 4:9 Comment(2)
${servletContext} doesn't exist. There's ${pageContext.servletContext}, but that isn't what you want. See also stackoverflow.com/tags/el/info and docs.oracle.com/javaee/5/tutorial/doc/bnahq.html#bnaijFencesitter
Ahhh, thanks very much. That clears it up... my JSP is getting rusty!Caesura

© 2022 - 2024 — McMap. All rights reserved.