如何从JavaScript调用servlet
web.xml中的Servlet配置
<servlet> <description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DataEntry</servlet-name>
<servlet-class>com.ctn.origin.connection.DataEntry</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DataEntry</servlet-name>
<url-pattern>/dataentry</url-pattern>
</servlet-mapping>
Javascript:
<script type="text/javascript"> function unloadEvt() {
document.location.href='/test/dataentry';
}
</script>
但是使用此javascript不能调用我的servlet。
有什么错误吗?如何调用servlet?
回答:
这可以使用ajax来完成
<script type="text/javascript"> function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "/testmail/dataentry", true);
xmlhttp.send();
}
</script>
以上是 如何从JavaScript调用servlet 的全部内容, 来源链接: utcz.com/qa/408339.html