我想在 javascript 中使用 java servlet。目前我在 javascript 中使用此代码:
var req = new XMLHttpRequest();
req.open("GET", "http://localhost:8080/FPvisualizer/test.java" + "?action=test", true);
req.send(null);
req.onreadystatechange = function() {processRequest()};
function processRequest() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById("target").innerHTML = req.responseText;
}
}
}
它与这个 java servlet 通信:
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class LoadOntology2 extends HttpServlet{
public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<message>valid</message>");
}
}
req.responseText 包含 servlet 文件的全部内容(即该文件的所有代码都显示在网页上)。有人知道我在这里做错了什么吗?