0

This is an error that came up after I got a previous question here answered. Essentially I'm trying to bring the absPath of the folder with me so I can make some Files at runtime regardless of where my code is located. I was recommended to use

getServletContext().getRealPath("/");

To find the correct path.

I tried it in my JSP page, but I get an unterminated string literal right when I create the absPath variable. Here is the script I try to run.

<script type="text/javascript">
        var RemoteUserId = "<%=(request.getRemoteUser()==null)? "blah" : request.getRemoteUser()%>";
        var contextPath = "<%= request.getContextPath()%>";         
        var extPath = "<%=extPath%>";
        var absPath = "<%=getServletContext().getRealPath("/")%>";
        var env='<%=string1%>';
</script>

What am I missing? Do I have to escape the returned path name, or did I misinterpret when I was supposed to use this.

Edit** This is what the source shows upon accessing the page
(slightly tweaked so I'm not showing my full C: path)

<script type="text/javascript">
        var RemoteUserId = "blah";
        var contextPath = "/TRACK";         
        var extPath = "http://xxx/sales/it/tlp/ext-3.2.1";
        var env='null';
        var absPath = "C:\Documents and Settings...\TRACK\";
</script>
B.Z.B
  • 471
  • 2
  • 10
  • 24
  • What is the generated output? Rightclick page, view source. Also, why do you need this as JS variable? Why don't you just determine it in the server side? I don't see how this information is useful for JS/client as it has no access to it anyway. – BalusC Mar 16 '11 at 19:19
  • I guess because I wasn't sure how to set it up server side. The project is pretty big, and I wasn't sure what I needed to add so that I could start up the server from a class instead of just starting up Tomcat from the servers view. I was going to pass this path into an ajax request (since we do it already from the framework setup) and from there I could access it on the java side. – B.Z.B Mar 16 '11 at 19:29
  • Just access it in the Java side once the request comes in. – BalusC Mar 16 '11 at 19:32
  • Could I see a snippet of code for this? Or a link to an example. With the way the framework is setup, the request is passed via an ajax request from the Ext-JS... which I would need to populate with params (like this absPath). – B.Z.B Mar 16 '11 at 19:52
  • It's not clear what the functional requirement is and what framework you're using which made that you're unable to just use `ServletContext#getRealPath()` in the Java code which should process this request. By the way, your problem is caused by backslashes. They are escape characters in JS strings as well. But after all, you should really not have the need to pass a Java variable as JS variable back to Java while you can access it in Java side itself as good. – BalusC Mar 16 '11 at 19:58

1 Answers1

0

Like as in Java, backslashes are escape characters in JS. You need to escape them to represent a literal backslash. In other words, your absPath variable must end up as

var absPath = "C:\\Documents and Settings...\\TRACK\\";

You could do this by

var absPath = "<%=getServletContext().getRealPath("/").replace("\\", "\\\\")%>";

But still, it makes no sense to me to pass a Java variable back to Java via JavaScript. Just access it in the Java side when the code is about to process the request. Imagine that you're using a servlet to process the request, just do

String absPath = getServletContext().getRealPath("/");

instead of

String absPath = request.getParameter("absPath");

(or whatever you're doing to get the absPath back in your Java code)

Also note that all the JavaScript code is fully controllable/spoofable/hackable by the client. The client is able to edit JavaScript variables while the code is running. The client could for instance change the path before it is been used. Keep this in mind!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I guess I need to figure out WHERE the Servlet is processing the request in my code, and understand the request / servlet / response relationship better... so I could agree with you =). – B.Z.B Mar 16 '11 at 20:10
  • Here are some links which should help you to understand JSP/Servlets: http://stackoverflow.com/tags/jsp/info, http://stackoverflow.com/tags/servlets/info and http://stackoverflow.com/questions/3106452/java-servlet-instantiation-and-session-variables/3106909#3106909 – BalusC Mar 16 '11 at 20:12