2

I want to get the value of name from javascript and pass to the string variable of java and then print it. How would I do that, I tried this code below but I have an error

Syntax error, insert ";" to complete LocalVariableDeclarationStatement

125:
126: String mname = "" %>var name = mmName;<%; 127:

<script type="text/javascript">
        function EditMerchantModal(mmName){
            var name = mmName;
            <%String mname = "" %>name;<%;
            System.out.println("the name of name is: "+mname);
            %>

        } 
    </script>

when I try this <%String mname = %>name;<%; and this <%String mname = ""+%>name;<%; the semicolon and plus sign are giving an error.

4
  • Can you give us more details as to why you are trying to do this? If the goal is to use it in some sort of an if-else construct or loop etc, you can use JSTL. Commented Jul 15, 2016 at 4:50
  • where do you think System.out.println would print to, when called from a browser? Commented Jul 15, 2016 at 4:54
  • I will use the value of string later for some data manipulation for my server, I just try to print the value to see if I already received it. but there in an error when I tried it to run. @ScaryWombat, it will print at the console. by the way Im using spring mvc. Commented Jul 15, 2016 at 5:05
  • thanks for responding. Commented Jul 15, 2016 at 5:09

2 Answers 2

3

You cannot simply take values from javascript variable to your jsp because your javascript values are client-side, your scriptlet is running on the server-side.

So if you want to use your javascript variables in a your JSP, then you will need to submit them.

refer to this question for more details

Coming to your question, this line

<%String mname = %>

is giving you an error because it's expecting a complete statement and so it prompts you to put a semicolon. However if you simply put a semicolon, then because of your assignment operator, it's expecting you to give a value to that variable.

When you do something like this

String mname = ""+

then because of your + (String concatenation) operator, it's expecting a second value that it requires in order to perform a concatenation.

I recommend that you should change your approach because the code inside your <% .. %> tags is running on server, and the code inside your <script> .. <script> is running when the file is at client's machine. So it simply won't work with your current approach.

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

1 Comment

Now I understand why it is not working. thanks for your help @Raman Sahasi. I used another way here to work it out using DOM manipulation.. :) thank you.
1
console.log('the name is : '+name);

Simply use this to check u have recieved the value or not instead of the scriptlet. Note that the value will now be printed on the browsers console. press F12 on the page to open console and check the value.

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.