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

查看:22
本文介绍了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 显示时,它输出的是:a$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 Spec 说:当 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 配置
  • 页面指令 isELIgnored (<%@ page isELIgnored="true|false" %>)

(请参阅 JSP 规范 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天全站免登陆