0
我对JSP进行了AJAX调用,JSP又调用了独立Java类中的Java方法。所有文件都在同一个文件夹中。将JSP值返回给AJAX调用
出于某种原因,我无法获得正确的值返回给AJAX。它只是打印整个JSP内容。
的JavaScript:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(true){
alert('hello!');
var response = xhr.responseText;
alert(response);
document.getElementById('newgame').innerHTML = xhr.responseText;
}
};
xhr.open('GET', 'javaconnect.jsp', true);
xhr.send(null);
JSP:
<%@ page import="com.example.Server"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Server tc = new Server();
out.print(tc.highScore());
%>
</body>
</html>
Java类:
package com.example;
public class Server {
public String highScore() {
return "hello!!!";
}
}
只是为了澄清,因为我对此很新,你建议我使用一个jsp的servlet INSTEAD? ajax调用中的URL将仅仅是servlet文件的名称no?就像我对jsp所做的一样。 –
如果你的Web项目使用的Servlet 3.0很简单: @WebServlet( “/我的小服务程序”)// <--------映射的Servlet 公共类MyServlet延伸的HttpServlet { ... } 的url将会是http:// localhost://my-servlet 但是使用Servlet 2.5你需要在web.xml文件中映射Servlet,这里是一个示例 https://docs.oracle。 COM/CD/E11035_01/wls100/web应用/ configureservlet.html –
emichels