I am fairly new to java and am trying to get the solution posted here working: How to use Servlets and Ajax?, but the jQuery call is not executing the servlet.
Servlet code is as follows:
package com.morley.app;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text";
System.out.println("Im working");
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(text); // Write response body.
}
}
My web.xml mappings are as follows:
<servlet>
<servlet-name>AjaxTest</servlet-name>
<servlet-class>com.test.app.AjaxTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxTest</servlet-name>
<url-pattern>/AjaxTest</url-pattern>
</servlet-mapping>
And my jsp is as follows:
<head>
<title>SO question 4112686</title>
<script type="text/javascript" src="<%= request.getContextPath() %>/js/jquery-latest.min.js"></script>
<script>
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function...
$('#somebutton').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
$.get('AjaxTest', function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
//alert('ready');
$('#somediv').text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
});
</script>
</head>
<body>
<button id="somebutton">press here</button>
<div id="somediv"></div>
</body>
If anyone can help me figure out why the doGet isn't firing it would greatly appreciated