Skip to content Skip to sidebar Skip to footer

Javascript String.replace(/\$/,str) Works Weirdly In Jsp File

For simplicity, i have the following file named test.jsp: I put this file in my local ser

Solution 1:

JSP 2.0 Spec says: "When EL evaluation is disabled, \$ will not be recognized as a quote, whereas when EL evaluation is enabled, \$ will be recognized as a quote for $." (JSP.3.3.2)

Whether EL Evaluation is enabled or disabled depends on many things:

  • if application server supports JSP 2.0 (Tomcat 5.0 and higher does)
  • web.xml descriptor file... if declares (e.g. by not using web-app_2_4.xsd schema) that it uses Servet 2.3 spec or earlier, EL is disabled by default.
  • JSP Configuration <el-ignored>
  • Page directive isELIgnored (<%@ page isELIgnored=”true|false” %>)

(See JSP Spec 2.0, part JSP.3.3.2 for more details)

Simple way to check if EL is enabled/disabled is to use ${request} on your page. If you see '${request}' in output, EL is disabled. If you see something different, it is enabled.

You may want to change \$ (one backslash) to \\$ (two backslashes) to get \$ in output if you also use EL on your page.

If you see differences between local/remote server, their settings related to EL probably differ. Best what you can do is to 1) use JSP 2.0, Servlet 2.4 or later (enables EL), 2) run same web server on machines. (Btw, I suggest you upgrade Tomcat 5.0 to 5.5 at least).


Solution 2:

Maybe you can move the javascript code to a separate .js file? This will prevent the code from being interpreted as jsp.


Solution 3:

Peter's got it, and Simon's suggestion is also a good idea.

Additionally, you don't need to use regex to do a simple static-string replace. The JS idiom would be:

alert('a$b'.split('$').join('k'));

Post a Comment for "Javascript String.replace(/\$/,str) Works Weirdly In Jsp File"