For Web applications, front-end (JavaScript) and back-end (Jsp/servlet) can not be used to share data, only the back-end program (JSP) data output to generate the page to the front, the generated page of JavaScript code is likely to get the so-called JSP data. Similarly, JavaScript data can be obtained from JSP programs only if you submit JavaScript data to back-end JSP code.
How do you implement JSP data in a page's JavaScript or use a page's JavaScript data in a JSP?
One, the page of JavaScript data How to submit to the background of the JSP program
1. JavaScript data can be xxx.jsp?var1=aaa&var2=bbb as a URL parameter to the JSP program, in the JSP
<%string strvar1=request.getparameter ("var1");%>
You can get the data that the JavaScript script passes over;
2. Use JavaScript to pass data to the JSP program by adding hidden field information to the form and submitting the form.
Refer to the following script: <script language= "javascript" > <!-- JavaScript scripts, placed anywhere on the page Insertclick () function Gets the variable varmc, that the JSP passes to the page then you can modify the value of the variable in JavaScript and then through Post is submitted to the JSP program for use. Function insertclick () { var1 = document.all.mc.value; // Gets the variable value in the page form var1var1 = var1 + "name"; Document.insertForm.submit (); } //--> </script> <!-- html page form form, Position in HTML page not limited to --> <form name= "Insertform" " method=" Post " action=" yourjsp "> <!-- The following is to get the variable value passed in the JSP program --> <input type=" hidden " Name= "MC" value= "<%=varMC%>" > <input type= "button" value= "Submit" Onclick= "Insertclick ()" > </form>
Second, the page of JavaScript data How to use the background JSP data
This is simpler, using <%=strVar1%> in JavaScript scripts to pass data from a JSP program to a JavaScript script.
Refer to the following script:
<!--HTML page form form, placed in an HTML page is not limited to--> <form name= "Insertform" method= "post" action= "yourjsp" > <input Type= "hidden" name= "MC" value= "> </form> <script language=" JavaScript "> <!--JavaScript script , you can use JAVASCIRPT to get the variable VARMC that the JSP passes to the page, and then you can use the value of the variable in JavaScript and assign it to a hidden field in the form by using JavaScript script anywhere after the form is placed in the page. var1 = "<%=varMC%>"; Gets the variable value in the JSP document.all.mc.value = var1; --> </script>