Javascript String.replace(/ \ $ /,str)在jsp文件中很奇怪 [英] Javascript String.replace(/\$/,str) works weirdly in jsp file

查看:125
本文介绍了Javascript String.replace(/ \ $ /,str)在jsp文件中很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我有以下名为test.jsp的文件:

For simplicity, i have the following file named test.jsp:

<script language="javascript">
    alert("a$b".replace(/\$/g,"k"));
</script>

我把这个文件放在我的本地服务器上,然后使用firefox显示它: http:// localhost:8080 / myproj / test.jsp 。它工作正常,结果警告字符串是:

I put this file in my local server, then display it using firefox: http://localhost:8080/myproj/test.jsp. It works okay, the result alert string is:

 akb

但是当我把这个文件放在远程服务器上并使用相同的firefox来显示时,它输出的是:$ bk这对我来说是不正确的。

But when i put this file in a remote server and use the same firefox to display, it output is: a$bk which is incorrect for me.

然后我将test.jsp内容更改为:

Then i change the test.jsp content to:

<script language="javascript">
    alert("a$b".replace(/\\$/g,"k"));
</script>

请注意,我添加两个斜杠而不是一个斜杠。在这种情况下,它适用于远程服务器,但不适用于本地服务器。

Note here i add two slashes instead of one. In this case it works in the remote server but not in local one.

然后我将文件从test.jsp重命名为test.html,但内容相同作为我的第一个案例,即:

Then i rename the file from test.jsp to test.html, but the content is the same as my first case, i.e.:

<script language="javascript">
    alert("a$b".replace(/\$/g,"k"));
</script>

通过使用相同的firefox,它在两台服务器上都能正常工作。

By using the same firefox, it works okay in both servers.

但我的文件需要在jsp文件中嵌入javascript。我的文件不允许使用.html后缀。在这种情况下我应该怎么做一个可移植的javascript?

But my file needs to embed the javascript in jsp file. it is not allowed to use .html suffix for my file. What should I do to write a portable javascript in this scenario?

我的本​​地服务器使用tomcat-5.5.26;远程服务器使用tomcat-5.0.28。 Firefox版本为3.0.4。

My local server uses tomcat-5.5.26; remote server uses tomcat-5.0.28. Firefox version is 3.0.4.

推荐答案

JSP 2.0规范说:当EL评估被禁用时,\ $将不会被识别为报价,而当启用EL评估时,\ $将被识别为$的报价。 (JSP.3.3.2)

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)

启用或禁用EL评估取决于很多方面:

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


  • 如果应用服务器支持JSP 2.0(Tomcat 5.0及更高版本)

  • web.xml描述符文件...如果声明(例如,不使用web-app_2_4.xsd架构)它使用Servet 2.3规范或更早版本,默认情况下禁用EL。

  • JSP配置< el-ignored>

  • 页面指令isELIgnored (<%@ page isELIgnored =true | false%>

  • 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" %>)

(有关详细信息,请参阅 JSP Spec 2.0 ,部分JSP.3.3.2) )

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

检查EL是否启用/禁用的简单方法是在页面上使用$ {request}。如果在输出中看到$ {request},则EL被禁用。如果您看到不同的内容,则会启用它。

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.

您可能想要更改 \ $ (一个反斜杠)到 \\ $ (两个反斜杠)在输出中获得 \ $ 如果你也使用EL页面。

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

如果您发现本地/远程服务器之间存在差异,则与EL相关的设置可能会有所不同。最好的办法是1)使用JSP 2.0,Servlet 2.4或更高版本(启用EL),2)在机器上运行相同的Web服务器。 (顺便说一句,我建议你至少将Tomcat 5.0升级到5.5)。

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).

这篇关于Javascript String.replace(/ \ $ /,str)在jsp文件中很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆