下面是jquery脚本,它通过URL调用servlet。
我有两个独立的项目。
首先是动态Web项目,它包含servlet等。
其次是简单的Ratchet HTML-CSS-JS项目,它当然包含一些页面,脚本和CSS。
<script>
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function...
$('#button').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
$.get('http://localhost:8080/testuje/text', function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
$('#div').text(responseText); // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
});
});
});
</script>
这是我的Servlet代码:
package pl.javastart.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/text")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String text = "some text";
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.
}
}
问题是,在点击按钮后,我应该在$.get('http://localhost:8080/testuje/text', function(responseText)
放入什么来获取servlet内容。