Implementing JSP data and JavaScript data interaction using __div

Source: Internet
Author: User

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>

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.