我正在阅读一本书来学习ajax,因为我的Servlet响应(只是文本)没有出现在我的JavaScript警报中,所以我陷入困境。单击该按钮时,它会调用JavaScript函数,然后异步发送http请求。
我不明白为什么String没有显示在响应中。感谢任何人都可以解决这个问题。
编辑:此外,在发送" get"之后,我得到状态代码200请求。当我在控制台下查看firebug时,我看到带有200状态代码的Get请求。有3个选项卡 - Params,Headers和XML。在Params下我看到我在请求中发送的虚拟参数。在标题下有2个节点 - 响应标头和请求标头。当我展开Response Header节点时,我看到一个名为content-length的东西,后面有一个0。这是否意味着我的回答中没有任何内容?
这是我的HTML
<html>
<head>
<title>Boards 'R' Us</title>
<link rel="stylesheet" type="text/css" href="boards.css" media="screen" />
<script type="text/javascript" src="ajax.js"> </script>
<script type="text/javascript" src="text-utils.js"> </script>
<script type="text/javascript" src="boards.js"> </script>
</head>
<body>
<h1>Boards 'R' Us :: How Much Butt We're Kicking</h1>
<div id="boards">
<table>
<tr><th>Snowboards Sold</th>
<td><span id="boards-sold">1672</span></td></tr>
<tr><th>What I Sell 'em For</th>
<td>$<span id="boards-price">249.95</span></td></tr>
<tr><th>What it Costs Me</th>
<td>$<span id="boards-cost">84.22</span></td></tr>
</table>
<table>
<tr><th>Boots Sold</th>
<td><span id="boots-sold">312</span></td></tr>
<tr><th>What I Sell 'em For</th>
<td>$<span id="boots-price">175.47</span></td></tr>
<tr><th>What it Costs Me</th>
<td>$<span id="boots-cost">54.23</span></td></tr>
</table>
<table>
<tr><th>Bindings Sold</th>
<td><span id="bindings-sold">82</span></td></tr>
<tr><th>What I Sell 'em For</th>
<td>$<span id="bindings-price">146.92</span></td></tr>
<tr><th>What it Costs Me</th>
<td>$<span id="bindings-cost">98.03</span></td></tr>
</table>
<h2>Cash for the Slopes:
$<span id="cash">318936.42</span></h2>
<form>
<input value="Show Me the Money" type="button"
onClick="getNewTotals();" />
</form>
</div>
</body>
</html>
和我的JavaScript
function getNewTotals() {
console.log("Getting new totals");
var url = "boards.do";
url = url + "?dummy=" + new Date().getTime();
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var reply = request.responseText;
alert("returned: " + reply);
} else {
alert("Error! Request status = " + request.status);
}
}
}
和我的Servlet
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gmail.gmjord.controller.BoardSalesController;
/**
* Servlet implementation class BoardSalesController
*/
//@WebServlet("/BoardSalesController")
public class BoardSalesController extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(BoardSalesController.class.getClass().getName());
private static final Level INFO = Level.INFO;
private static final Level[] LEVELS = {Level.SEVERE, Level.WARNING, Level.INFO, Level.CONFIG,
Level.FINE, Level.FINER, Level.FINEST};
/**
* @see HttpServlet#HttpServlet()
*/
public BoardSalesController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
LOGGER.log(INFO, "in doGet()");
String info = request.getParameter("dummy");
LOGGER.log(INFO, "Request info: " + info);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
String reply = "Do you see this?";
response.getWriter().write(reply);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
我了解到您正在正确调用servlet
并获得http-header
状态= 200的响应,但是您没有收到字符串内容,不是吗?
然后我认为你必须在编写器上调用flush()
来提交响应。如果您查看getWriter()
中javax.servlet.ServletResponse
javax.servlet.http.HttpServletResponse
servlet container
的界面,您可以看到:
在PrintWriter上调用flush()会提交响应。
然而,这通常不是必需的,因为doGet
为您做了这些,但是在您的情况下似乎不是这样尝试在您的servlet上的String reply = "Do you see this?";
PrintWriter writer = response.getWriter();
writer.write(reply);
writer.flush();
方法中使用此代码:
{{1}}
希望这有帮助,