0

I want to update a JSP variable value from a Java Script code (JS code is in different file). Means a JSP variable to be updated from a included .JS file code

2
  • 1
    No. That is not possible. However your request data (via AJAX) can be received by the Servlet/JSPs. Commented Aug 17, 2012 at 7:44
  • in fact you can do that pretty easily, need more code for that. Commented Aug 17, 2012 at 8:11

2 Answers 2

3

It can't be done. JSP is executed on the server and JavaScript on the browser.

What you can instead do, is send an HTTP (GET or POST) request to the JSP and use it to update the variable.

Sign up to request clarification or add additional context in comments.

Comments

2

JSP is run server-side (on the server), whereas JavaScript is run client-side (in the browser). The two can't communicate directly.

You can create a JavaScript variable with the value of your JSP variable (in your JSP file):

<script type="text/javascript">
    var myJspVariable = '<%= myJspVariable %>';
</script>
<script type="text/javascript" src="myJavascriptFile.js"></script>

then you can work with myJspVariable in your other .js file. However, that won't update the value on the server - if you need to do that, you'll have to make an AJAX request to post the value back.

Comments

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.