Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript removes slash from JSP variable

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}">

like image 879
sergionni Avatar asked Dec 29 '22 02:12

sergionni


1 Answers

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}")
    + "',";
like image 112
BalusC Avatar answered Jan 08 '23 16:01

BalusC