Interaction between js and jsp .........

Source: Internet
Author: User

A solution to the problem of JSP data and JavaScirpt Data Interaction <br/> for WEB programs, front-end (JavaScript) and backend (JSP/Servlet) cannot share data, 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. </P> <p> how can we use jsp data in page JavaScript or page JavaScript data in jsp? </P> <p> 1. How do I submit JavaScript data on the page to the jsp program in the background? <br/> ① can I use xxx. JSP as the JavaScript data? Var1 = aaa & var2 = bbb is used as URL parameters to pass to the JSP program. In this case, <br/> <% String strVar1 = request. getParameter ("var1"); %> to get the data passed by the JavaScript script; <br/> ② use JavaScript to add hidden domain information to the form, then, the data is transmitted to the JSP program through form submission. </P> <p> refer to the following script: <br/> <mce: script language = "JavaScript"> <! -- </P> <p> /******************************* ******************************* </p> <p> JavaScript script, you can use the insertclick () function to obtain the variable varMC passed to the page by JSP, <br/> then, you can modify the value of this variable in JavaScript, and submit it to the JSP program in the form of <br/> post. <Br/> ************************************ * *************************/<br/> function insertclick () {<br/> var1 = document. all. mc. value; // get the variable value in form <br/> var1 = var1 + "name"; <br/> document. insertForm. submit (); <br/>}< br/> // --> </mce: script> </p> <! -- Html page form, the location on the html page is unlimited --> <br/> <form name = "insertForm" method = "post" action = "yourJSP"> <br/> <! -- The following sentence gets the variable value passed in the JSP program --> <br/> <input type = "hidden" name = "mc" value = "<% = varMC %> "> <br/> <input type =" button "value =" Submit "onclick =" insertclick () "> <br/> </form> </p> <p> 2. How to use the data of JSP programs in the background for JavaScript data on the page <br/> This is relatively simple, you can directly use <% = strVar1 %> In the JavaScript script to pass the data in the jsp program to the JavaScript script. </P> <p> refer to the following Script: </p> <! -- Html page form, the location on the html page is unlimited --> <br/> <form name = "insertForm" method = "post" action = "yourJSP"> <br/> <input type =" hidden "name =" mc "value =" "> <br/> </form> </p> <mce: script language = "JavaScript"> <! -- </P> <p> /******************************* ******************************* </p> <p> JavaScript script, you can use JavaScirpt to obtain the variable varMC passed to the page by Using JSP, <br/> then you can use the value of this variable in JavaScript and assign a value to the hidden field in form through the <br/> JavaScript script. <Br/> ************************************ * *************************/<br/> var1 = "<% = varMC %> "; // obtain the variable value in JSP <br/> document. all. mc. value = var1; <br/> // --> </mce: script> </p> <p> quotation marks must be placed here ..........

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.