JerVi JerVi - 6 months ago 25
Java Question

how to get the variable value of javascript to be pass to the string of java in jsp

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


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

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


when I try this
<%String mname = %>name;<%;

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


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.