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

Alibaba Cloud 10 Year Anniversary

With You, We are Shaping a Digital World, 2009-2019

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.