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.

JerVi
  • 131
  • 3
  • 14
  • 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](https://jstl.java.net/). – Aditya Gupta Jul 15 '16 at 04:50
  • where do you think `System.out.println` would print to, when called from a browser? – Scary Wombat Jul 15 '16 at 04: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. – JerVi Jul 15 '16 at 05:05
  • thanks for responding. – JerVi Jul 15 '16 at 05:09

2 Answers2

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.

Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • 1
    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. – JerVi Jul 15 '16 at 06:03
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.

Utkarsh
  • 60
  • 1
  • 1
  • 7