Implementation of interactive use of JSP data and JavaScript data

Source: Internet
Author: User

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

<%String strVar1=request.getParameter("var1");%>

You 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脚本,放置在页面中的任何位置都可以
* insertclick()函数获取JSP传递到页面中的变量varMC,
* 然后就可以在JavaScript中修改这个变量的值,再通过
* post的方式提交给JSP程序来使用。
***************************************************************/
function insertclick(){
var1 = document.all.mc.value; //获取页面form中的变量值
var1 = var1 + "名称";
document.insertForm.submit();
}
//-->
</script>
<!-- html页面form表单,放置在html页面中的位置不限 -->
<form name="insertForm" method="post" action="yourJSP">
<!-- 下面这一句是获取JSP程序中传递过来的变量值 -->
<input type="hidden" name="mc" value="<%=varMC%>">
<input type="button" value="提交" onclick="insertclick()">
</form>

Second, the page of JavaScript data How to use the background of the JSP program data

This is simpler and can be used directly in JavaScript scripts to pass data from a JSP program to a JavaScript script.

Refer to the following script:

<!-- html页面form表单,放置在html页面中的位置不限 -->
<form name="insertForm" method="post" action="yourJSP">
<input type="hidden" name="mc" value="">
</form>
<script language="JavaScript">
<!--
/***************************************************************
* JavaScript脚本,放置在页面中form以后的任何位置都可以
* 使用JavaScirpt获取JSP传递到页面中的变量varMC,
* 然后就可以在JavaScript中使用这个变量的值,通过
* JavaScript脚本赋值给form中的隐藏域。
***************************************************************/
var1 = "<%=varMC%>"; //获取JSP中的变量值
document.all.mc.value = var1;
//-->
</script>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.