Java jquery ajax调用servlet方法并返回json数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19739476/
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
jquery ajax calling servlet method and return json data
提问by sarath
I am new to Ajax programming I am using Jquery ajax my requirement to to fetch the data into server and display as table format.In that location must display radio button type. how to call servlet specific method how to return data.i am ready to use json.can any one help me how to call a method how to return a data.and suggestions require to solve the problem
我是 Ajax 编程的新手我正在使用 Jquery ajax 我要求将数据提取到服务器并显示为表格格式。在该位置必须显示单选按钮类型。如何调用servlet特定方法如何返回数据。我准备使用json。有人可以帮助我如何调用方法如何返回数据。解决问题的建议需要
thanks in advance.
提前致谢。
$('#ajaxbutton').on('click',function(){
$.ajax({
type:"post",
url:"Db2",
data:{"labid",100},
sucess:function(){
alert("sucess");
}
});
});
in servlet
在小服务程序中
public class Db2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws MalformedURLException, IOException {
doProcess(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws MalformedURLException, IOException {
doProcess(request, response);
}
public void doProcess(HttpServletRequest request,
HttpServletResponse response) throws MalformedURLException,
IOException {
Connection con;
PreparedStatement ps, ps1;
PrintWriter out = response.getWriter();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:oracle1", "system",
"sarath");
ps = con.prepareStatement("select trackid,location,to_char(mydate,'dd-mon-yyyy') from information where labid=100");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String location = rs.getString(2);
String track = rs.getString(1);
String myDate = rs.getString(3);
}
} catch (Exception e) {
out.println(e);
}
}
}// end of ServletB
采纳答案by heqing
PrintWriter out = response.getWriter();
StringBuffer res = new StringBuffer();
while (rs.next()) {
String location = rs.getString(2);
String track = rs.getString(1);
String myDate = rs.getString(3);
res.append("{");
res.append("'location':");
res.append(location);
res.append(",");
res.append("'track':");
res.append(track);
res.append(",");
res.append("'myDate ':");
res.append("myDate ");
res.append("}");
}
out.println(res.toString());
must be json-string and the ajax can use eval("data = "+r_data+";");
必须是 json-string 并且 ajax 可以使用 eval("data = "+r_data+";");
回答by SpringLearner
how to call servlet specific methodIn your ajax you are writing type=post
so in the servlet doPost()
will be called.if it would have been type=get
then servlet doGet()
method will be called.So you should write the database retrieve part in the specific method.
如何调用servlet特定方法在你正在编写的ajaxtype=post
中,servletdoPost()
将被调用。如果它已经被调用,type=get
那么servletdoGet()
方法将被调用。所以你应该在特定方法中编写数据库检索部分。
Now suppose you want location,track and mydate then try this way In the servlet
现在假设您想要位置、跟踪和我的日期,然后在 servlet 中尝试这种方式
PrintWriter out.response.getWriter();
out.println(location);
out.println(track);
out.println(mydate);
In the ajax success part do this way
在ajax成功部分这样做
success: function(data, textStatus, jqXHR){
alert(data);