0

I want to access a system property from my .js file. Initially I had accessed the system property from my jsp file using the below syntax which worked just fine:

<script type="text/javascript">
 function ChatWindow(){
        var property = "<%=System.getProperty("CHAT_WINDOW_URL") %>";
        alert(property);
    }
    </script>

However when I tried to use the same function in .js file, I am getting errors that :

Expected ';'

When I do add ';' as follows:

var property = "<%=System.getProperty("CHAT_WINDOW_URL"); %>";

or as

var property = "<%=System.getProperty('CHAT_WINDOW_URL') %>";

The error goes.. but the property value is not resolved. Could anybody please help me on this.

6
  • are there any errors on browser console? Commented Apr 30, 2014 at 6:09
  • 3
    you DO realize that JS code runs on the client side and JSP gets compiled on the server side, so its not a good practise to mix them both as you have done. Commented Apr 30, 2014 at 6:09
  • 1
    I doubt your servlet is going to parse JSP code inside a .js file. Why not just revert back to setting the JS variable in your JSP file? Commented Apr 30, 2014 at 6:14
  • Actually i have 4 jsp files trying to use the same function, hence i thought it would be better to write the function in a common js file.Is it not possible to write java code in a js file? Commented Apr 30, 2014 at 6:20
  • 1
    Better you can have a common jsp file instead of js and include it in rest of the files. This will solve your issue. Commented Apr 30, 2014 at 6:24

1 Answer 1

2

This is not a correct way to do it. Javascript is client side code whereas scriptlet is complied on server side. The best way to do this is by using a hidden input element.

<input type="hidden" value="<%=System.getProperty('CHAT_WINDOW_URL') %>" id="chatWindowURL" ../>

Now, in you javascript, write:

var systemURL = document.getElementById('chatWindowURL').value;
Sign up to request clarification or add additional context in comments.

1 Comment

Or just set the systemURL variable in the JSP file before the external <script> tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.