Interaction between JSP data and javascirpt data

Source: Internet
Author: User
One solution to the problem of JSP Data Interaction and use of javascirpt data is that front-end (JavaScript) and backend (JSP/servlet) cannot share data for web programs, only the back-end program (JSP) can output data and generate pages to the front-end. At this time, the JavaScript code in the generated page may obtain the so-called JSP data. Similarly, JavaScript data can be obtained in JSP programs only when the data in Javascript is submitted to the backend JSP code. So how can we use JSP data in page JavaScript or page JavaScript data in JSP? I. How can I submit JavaScript data on the page to the JSP program in the background? ① can I use XXX. jsp as JavaScript data? Var1 = aaa & var2 = BBB is used as URL parameters to pass to the JSP program. In this case, <% string strvar1 = request is used in JSP. getparameter ("var1"); %> to get the data passed by the Javascript script. ② use JavaScript to add hidden domain information to the form, then, the data is transmitted to the JSP program through form submission. See the following Script: <script language = "JavaScript"> <! --/************************************** * ************************* Javascript script, you can use the * insertclick () function to retrieve the variable varmc passed to the page by JSP at any location on the page. * then you can modify the value of this variable in JavaScript, then, use the * POST method to submit it to the JSP program for use. **************************************** * *********************/Function insertclick () {var1 = document. all. MC. value; // obtain the variable value var1 = var1 + "name"; document. insertform. submit () ;}// --> </SCRIPT> <! -- Form on the HTML page, which has no restrictions on the location of the form on the HTML page --> <form name = "insertform" method = "Post" Action = "yourjsp"> <! -- The following sentence gets the variable value passed in the JSP program --> <input type = "hidden" name = "MC" value = "<% = varmc %>"> <input type = "button" value = "Submit" onclick = "insertclick () "> </form> 2. It is relatively simple to use the data of JSP programs in the background for JavaScript data on the page, you can directly use <% = strvar1 %> In the Javascript script to pass the data in the JSP program to the Javascript script. See the following Script: <! -- HTML page form, the location on the HTML page is unlimited --> <form name = "insertform" method = "Post" Action = "yourjsp"> <input type = "hidden" name = "MC" Value = ""> </form> <script language = "JavaScript"> <! --/************************************** * ************************* Javascript script, in the form field on the page, you can * use cirpt to obtain the variable varmc passed to the page by JSP. * then you can use the variable value in JavaScript, use the * Javascript script to assign values to the hidden fields in the form. **************************************** * *********************/Var1 = "<% = varmc %> "; // obtain the variable value document in JSP. all. MC. value = var1; // --> </SCRIPT>
Transferred from: Baidu Knows

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.