views:

312

answers:

4

Have JSP variable ${remoteFolder}

It's value is \\file-srv\demo

Use jQuery embedded in this JSP.

jQuery resolves ${remoteFolder} variable as \file-srvdemo ,i.e. one slash is removed.

How to remain initial value of this var?

edited: when ${remoteFolder} is used inside form tag, that it resolved OK.

edited2:

JS part of JSP, slashes are stripped out..

  <script>
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.text = "var $j = jQuery.noConflict();";
        oScript.text+= "$j(document).ready(function(){";
        ...
       oScript.text+= "'script':'<%= request.getContextPath()   %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}',";
        ...
        oScript.text+= "});"; 
        document.body.appendChild(oScript);        
    </script>

edited3:

earlier usage of ${remoteFolder} var,that was all OK with slashes < form enctype="multipart/form-data" method="post" target="uploadFrame" action="<%= request.getContextPath() %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}">

A: 

Try setting $remotefolder to \\\\file-srv\\demo since javascripts interprets \ as escape char, thereby needing \\ to print \.

kb
it came from backend and in form tah it's resolved ok..,but in jQuery ready function it strripped the slash
sergionni
+1  A: 

That's JavaScript rather than jQuery. You'll need to escape any backslash you want to be preserved when creating JavaScript string literals using an extra backslash.

Tim Down
inside form tag inside action attribute this variable contains all slashes OK, strange..
sergionni
Could you post the relevant JavaScript that your JSP is generating?
Tim Down
see "edited2" section ,please, there is a chunk of code ,where slashes unexpectedly stripped - it's jQuery part of "uploadify" plugin
sergionni
Sorry, I wasn't clear. I meant the relevant snippet of the HTML source of the page as served to the browser, not the JSP.
Tim Down
if you mean servlet part,that UI is encapsulated in JSP only,our Servlet and Portlets do logic work only.Is that you mean? thank you.
sergionni
i mentioned that HTML contained in JSP only
sergionni
perhaps, to use Javascript base64?
sergionni
You could use base 64, but you'd need to write/find a decoding function for that (not every browser has a built-in base 64 decoding function). Escaping every backslash in your Java string with an extra backslash is all you need. What I was trying to ask was what you see if you use "view source" in your browser, so that we could be sure that it is the backslash escaping problem.
Tim Down
i see \file-srvdemo instead of \\file-srv\demo
sergionni
+1  A: 

There are two problems here.

First, the \ is an escape character in JS strings. When you want to represent a \ in a JS string, you need to double-escape it: \\. Easiest way would be using JSTL fn:replace for this.

var jsVariable = "${fn:replace(javaVariable, '\\', '\\\\')}";

Second, you want send it as an URL parameter. The \ is an illegal character in URL parameter. You need to URL-encode it. Easiest way would be using Javascript's escape() function for this.

var urlParameter = escape(jsVariable);

Summarized, the

oScript.text+= "'script':'<%= request.getContextPath()   %>/uploadFile?portletId=${portletId}&remoteFolder=${remoteFolder}',";

needs to be replaced by

oScript.text += "'script':"
    + "'${pageContext.request.contextPath}/uploadFile"
    + "?portletId=${portletId}"
    + "&remoteFolder=" + escape("${fn:replace(remoteFolder, '\\', '\\\\')}")
    + "',";

Alternatively, you can just use / instead of \ as file path separator. This works perfectly in Windows as well. You don't need to escape them for use in strings, you however still need to URL-encode it.

oScript.text += "'script':"
    + "'${pageContext.request.contextPath}/uploadFile"
    + "?portletId=${portletId}"
    + "&remoteFolder=" + escape("${remoteFolder}")
    + "',";
BalusC
The problem is that just replacing the backslash isn't necessarily enough. What if there's a double-quote character in the string? Or two-byte Unicode characters? Well in this case it's a file name, so maybe it's safe to make an assumption or two.
Pointy
BalusC, thank you for clarification. I will try this way.Initially i tried escaping, but /\f gave me symbol looked like square,when i check this string in notepad.
sergionni
+1  A: 

I find that I cannot write a serious web application without having my own EL function library with some critical functions in it. Among those is a "jsQuote" (or "escapeJS", depending on what sort of mood I'm in) function, whose intent is to "protect" expanded strings so that they can be dropped into Javascript string constants. It's analogous to fn:escapeXml() but instead of targetting the HTML syntax it targets Javascript. Generally what it has to do is check for backslash, the quote characters, newline & the other common control characters, and then any characters outside the 7-bit printable range. With such a function, you can always safely do something like this:

<script>
  // ...
  var s = 'A string ${yourLib:escapeJS(some.java.bean.property)} constant';
  // ...
</script>

I really wish such a thing would become part of the JSTL standard, but I'm not hopeful. Luckily it's really easy to write.

Pointy
good idea actually, taking into consideration,that such cases oftenly occurred for web developers
sergionni