Reputation: 623
I am going through a book to learn ajax and I am stuck as my Servlet response (just text) is not showing up in my JavaScript in an alert. When the button is clicked it calls a JavaScript function and then an http request get sent asynchronously.
I don't understand why the String is not showing in the response. Thanks for any light anyone can shed on this.
EDIT: Also, I get a status code of 200 after sending the "get" request. When I look at firebug under console I see the Get request with 200 status code. There a 3 tabs - Params, Headers, and XML. Under Params I see my dummy parameter that I send in the request. Under Headers there are 2 nodes - Response Headers and Request Headers. When I expand the Response Header node I see a thing called content-length with a 0 after it. Does that mean there is nothing in my response???
Here is my 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>
and my 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);
}
}
}
and my 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
}
}
Upvotes: 2
Views: 2206
Reputation: 18517
I understand that you're calling the servlet
correctly and getting the response with an http-header
status = 200, however you aren't receiving the string content, isn't it?
Then I think you have to call flush()
on the writer to commit the response. If you check the getWriter()
in javax.servlet.ServletResponse
documentation which is the interface of javax.servlet.http.HttpServletResponse
you can see this:
Calling flush() on the PrintWriter commits the response.
However this is normally not necessary because servlet container
do that for you, however in your case seems not so try with this code in the doGet
method on your servlet:
String reply = "Do you see this?";
PrintWriter writer = response.getWriter();
writer.write(reply);
writer.flush();
Hope this helps,
Upvotes: 1