A solution to the problem of interactive use of JSP data and JAVASCIRPT data
For Web applications, front-end (JavaScript) and back-end (Jsp/servlet) are not able to share data, only the back-end program (JSP) to output data, generate pages to the front, The JavaScript code in the generated page is likely to get the so-called JSP data. In the same way, the JavaScript data can be obtained from the JSP program only if the data in JavaScript is submitted to the back-end JSP code.
How do you implement data from a JSP 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
① can pass JavaScript data to the JSP program as a xxx.jsp?var1=aaa&var2=bbb parameter in the form of a URL, in the JSP
The <%string strvar1=request.getparameter ("var1");%> can get the data that the JavaScript script passes over;
② uses JavaScript to pass data to a JSP program by adding hidden field information to the form, and then submitting the form.
Refer to the following script:
<script language= "JavaScript" >
<!--
/***************************************************************
JavaScript scripts that can be placed anywhere on the page
The Insertclick () function Gets the variable VARMC that the JSP passes to the page,
You can then modify the value of the variable in JavaScript, and then pass the
Post is submitted to the JSP program for use.
***************************************************************/
function Insertclick () {
var1 = Document.all.mc.value; Get the value of a variable in a page form
var1 = var1 + "name";
Document.insertForm.submit ();
}
-->
</script>
<!--HTML page form form, placed in the HTML page is not limited to the location of-->
<form name= "Insertform" method= "post" action= "yourjsp" >
<!--The following sentence is to get the variable value passed over 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 of the JSP program 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 the HTML page is not limited to the location of-->
<form name= "Insertform" method= "post" action= "yourjsp" >
<input type= "hidden" name= "MC" value= "" >
</form>
<script language= "JavaScript" >
<!--
/***************************************************************
JavaScript scripts that can be placed anywhere in the page after form
Use JAVASCIRPT to get the variable VARMC that the JSP passes to the page,
You can then use the value of this variable in JavaScript, by
The JavaScript script is assigned to a hidden field in the form.
***************************************************************/
var1 = "<%=varMC%>"; Get the value of a variable in a JSP
Document.all.mc.value = var1;
-->
</script>