A way for JavaScript to pass parameters to a JSP: using the XMLHTTP object

Source: Internet
Author: User
Tags object readline string version tomcat
javascript|js|xml| objects

The XMLHTTP object can be used to pass parameters without refreshing the page, as a way for JavaScript to pass parameters to the JSP.

Here's an example of JavaScript passing parameters to a JSP:

xmlhttp.jsp

<%@ page contenttype= "text/html; charset=gb2312 "language=" java import= "java.util.*" import= "java.sql.*"%>
<script language= "JavaScript" >
function func ()
{
XML = new ActiveXObject ("Microsoft.XMLHTTP");
var post= "<input type= ' text ' name= ' name ' value= ' 1value '/>";//construct the data to carry
Xml.open ("Post", "xmlhttp.jsp", false);//Use the POST method to open a connection to the server and communicate asynchronously
Xml.setrequestheader ("Content-length", post.length);
Xml.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xml.send (post);//Send data
return results
var v = xml.responsetext;
Deal with this result
document.write (' Result: ' + V ');

}
</script>
<body >
<%
Out.println ("ABCD");
Java.io.BufferedReader br = Request.getreader ();
String str = "";
String Srt=br.readline ();
while (Srt!=null)
{
STR + + srt+ "\ n";
Srt=br.readline ();
}
Out.println (str);
%>
</body>

This means that the parameter "<input type= ' text ' name= ' name ' value= ' 1value '/> ' can be passed to this page to display a text box on the page. Of course you can also pass an XML file so you need to parse the XML file in the server-side code to get the parameters you need

You can pass parameters to any page, just change the Xml.open ("POST", "xmlhttp.jsp", false), and the xmlhttp.jsp for your target page will be URL.

It should be noted that in the lower version of Tomcat will report the error of read time out, this is the reason for Tomcat, a higher version of the problem can be solved



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.