Interactive use of JSP data and JavaScript data

Source: Internet
Author: User

For WEB programs, front-end JavaScript) and back-end JSP/Servlet can not share data, only the back-end program JSP) to output data, 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 JavaScript data 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 to submit JavaScript data on the page to the JSP program in the background

1. JavaScript data can be stored in xxx. JSP? Var1 = aaa & var2 = bbb is used as URL parameters to pass to the JSP program.

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

The data transmitted by the JavaScript script can be obtained;

2. Use JavaScript to add hidden domain information to the form, and then pass the data to the JSP program by submitting the form.

Refer to the following script:

 
 
  1. <Script Language="JavaScript">
  2. <! --
  3. JavaScript script, which can be placed anywhere on the page
  4. The insertclick () function gets the variable varMC passed to the page by JSP,
  5. Then you can modify the value of this variable in JavaScript, and then use
  6. The post method is submitted to the JSP program for use.
  7. Function insertclick (){
  8. Var1=Document. All. mc. value; // get the variable value in form.
  9. Var1Var1= Var1 + "name ";
  10. Document. insertForm. submit ();
  11. }
  12. //-->
  13. </Script>
  14.  
  15. <! -- Html page form, which is placed on an html page without any restrictions -->
  16. <Form Name="InsertForm" Method="Post" Action="YourJSP">
  17. <! -- The following sentence gets the variable value passed in the JSP program -->
  18. <Input Type="Hidden" Name="Mc" Value="<% = VarMC %>">
  19. <Input Type="Button" Value="Submit" Onclick="Insertclick ()">
  20. </Form> 

Ii. How to use JSP data in the background for JavaScript data on the page

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.

Refer to the following script:

 
 
  1. <! -- Html page form, which is placed on an html page without any restrictions -->
  2. <Form Name="InsertForm" Method="Post" Action="YourJSP">
  3. <Input Type="Hidden" Name="Mc" Value="">
  4. </Form>
  5.  
  6. <Script Language="JavaScript">
  7. <! --
  8. JavaScript script, which can be placed anywhere after form in the page
  9. Use cirpt to obtain the variable varMC passed to the page by JSP,
  10. Then you can use the value of this variable in JavaScript.
  11. The JavaScript script assigns a value to the hidden field in form.
  12. Var1="<% = VarMC %>"; // Get the variable value in JSP
  13. Document. all. mc. value=Var1;
  14. //-->
  15. </Script> 
 
  1. Notes for connecting JSP to ORACLE databases
  2. Configuration of tomcat SQL Server2000 Database Connection Pool in JSP
  3. Differences between Servlets and JSP
  4. Differences between JSP include commands and include actions
  5. Solutions to JSP output of excel documents and Chinese garbled characters

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.