Example of passing between java and javascript variables
- 2020-12-26 05:50:30
- OfStack
Recently, I used jsp to make a website, which involved the exchange between java variable and javascript variable. Although there were a lot of online materials, opinions were varied and many of them were misleading. After many attempts, I finally understood the problem clearly. In layman's terms, this means that java code runs on the server and the returned value exists statically on the page, while javascript is a scripting language and runs on the client! They are two completely different languages. Variables cannot be exchanged directly, but they can be passed around in a fixed way.
The java variable is passed to the javascript variable, which is a simple, ordinary method:
Be careful not to omit the equals sign! There's nothing wrong with that.
The javascript variable is passed to the Javab variable, which is a little more complicated, but it is clear that the principle is also very simple. Here, it is obtained and operated mainly through form submission and form elements:
Pay special attention to the forms and javascript code sections! Remove the database through the form of hidden elements to a data to the processing function, after the relevant operation, submit the form, through request java code. getParameter (), will be OK ~ ~ ~ in fact was really tangled one problem is that the variable is not synchronous, because I am in order to realize the function of every click on the button 1 counter plus 1 and the database once a read operation, if the counter is not 1 to order problem will directly lead to read, refresh the page can be solve the problem, but a straight flush is not a good solution. So I'm going to pass in 1 parameter, OK. All right
The java variable is passed to the javascript variable, which is a simple, ordinary method:
var cnt = <%=number%>;
Be careful not to omit the equals sign! There's nothing wrong with that.
The javascript variable is passed to the Javab variable, which is a little more complicated, but it is clear that the principle is also very simple. Here, it is obtained and operated mainly through form submission and form elements:
<script language="javascript">
function button_click(cnt) {
++cnt;
document.submitForm.msg.value = cnt;
document.submitForm.submit();
}
</script>
<%
number = request.getParameter("msg");
if(number==null)
number="1";
RdData.setIndex(number);
RdData.readData();
out.println("<br />");
out.print(RdData.getIndex() + ".");
out.print(RdData.getTitle());
out.println("<br />");
out.println("A." + RdData.getStrA());
out.println("<br />");
out.print("B." + RdData.getStrB());
out.println("<br />");
out.print("C." + RdData.getStrC());
out.println("<br />");
out.print("D." + RdData.getStrD());
%>
<input name="index" type="text" value="<%=RdData.getIndex()%>" />
<input type="button" value="btn" name="bt" onclick="button_click(<%=number%>)">
<form name="submitForm">
<input type="hidden" name="msg" value=<%=RdData.getIndex()%>/>
</form>
Pay special attention to the forms and javascript code sections! Remove the database through the form of hidden elements to a data to the processing function, after the relevant operation, submit the form, through request java code. getParameter (), will be OK ~ ~ ~ in fact was really tangled one problem is that the variable is not synchronous, because I am in order to realize the function of every click on the button 1 counter plus 1 and the database once a read operation, if the counter is not 1 to order problem will directly lead to read, refresh the page can be solve the problem, but a straight flush is not a good solution. So I'm going to pass in 1 parameter, OK. All right