如何从java Servlet返回数据到ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18479477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to return data to ajax from java Servlet
提问by roland luo
i have my ajax function as follows:
我的ajax功能如下:
$.ajax({
type: 'GET',
url: "/myservlet",
data: {
objects: '2',
dimension: '2',
},
success: function( data ) {
console.log(data);
alert(data);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
and i have my servlet to process the data sent to /myservlet. I read from the ajax tutorial which said the data in the success function is the data that ajax got from the server side. But i don't know how to set this data or return this data from doGet method in a Java servlet to the frontend. It seems doGet is a void method and cannot return any values, isn't it? I am a freshman in web development, thanks in advance!
我有我的 servlet 来处理发送到 /myservlet 的数据。我从ajax教程中读到,成功函数中的数据是ajax从服务器端获取的数据。但我不知道如何设置这些数据或将这些数据从 Java servlet 中的 doGet 方法返回到前端。看起来 doGet 是一个 void 方法并且不能返回任何值,不是吗?我是网络开发的大一新生,提前致谢!
回答by Krushna
You no need to return any thing from the doGet
method , infact you can not as it's void.
你不需要从doGet
方法中返回任何东西,事实上你不能,因为它是无效的。
So what you need to do is get the PrintWriter
object from the response and write data to it and that will available in the success function .
所以你需要做的是PrintWriter
从响应中获取对象并将数据写入它,这将在成功函数中可用。
回答by Masudul
You can get the data from servlet by writing on response.getWriter().write("");
.
您可以通过写入从 servlet 获取数据response.getWriter().write("");
。
Here is a simple servlet example.
这是一个简单的 servlet 示例。
@WebServlet(name = "MyServlet", urlPatterns = {"/myservlet"})
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("Success Data");
}
}
回答by NickJ
You might notice that the doGet() method has two parameters: HttpServletRequest and HttpServletResponse.
您可能会注意到 doGet() 方法有两个参数:HttpServletRequest 和 HttpServletResponse。
You use HttpServletRequest to get information about the request - any parameters, the calling client IP, the URL etc.
您使用 HttpServletRequest 获取有关请求的信息 - 任何参数、调用客户端 IP、URL 等。
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html
You use HttpServletResponse to populate the response. HttpServletResponse has a number of methods allowing you to set response headers and data.
您使用 HttpServletResponse 来填充响应。HttpServletResponse 有许多方法允许您设置响应头和数据。
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html