使用jQuery Ajax从JSP页面调用Servlet [英] Calling a Servlet from a JSP page using jQuery Ajax

查看:88
本文介绍了使用jQuery Ajax从JSP页面调用Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多相关问题,但实际上没有与我的问题相关的问题附有答案.或至少是一个公认的答案.如果有人可以指出我的问题,很高兴在这方面做错了.另外,如果我错过了标签,请重新标记.

I know there are quite a few related question here but none that actually relate to my question have an answer attached. Or at least an accepted answer. Happy to be wrong in this if someone can point me to the question. Also please retag if I've miss-tagged.

我的问题,正如标题所述,我想从我的JSP页面调用Servlet并返回一个字符串或html.

My question, as the title says is that I want to call a Servlet from my JSP page and return a string, or html.

我的Servlet名称是MyFirstServlet.

My Servlet name is MyFirstServlet.

请问,由于我是一个完整的JSP,Java和Servlet使用者,所以请在回答中都非常具体.

Could you please, please be very specific in any answers as I am a complete JSP, Java and Servlet noobie.

非常感谢您.

推荐答案

首先创建一个Servlet类,该Servlet类根据请求返回所需的响应.它可以是HTML,XML或JSON.我建议为此使用 JSON ,因为这是最容易产生的Java,可在JavaScript中使用.例如,您可以使用 Google Gson 将有价值的Java对象转换为JSON字符串(反之亦然).例如

First create a Servlet class which returns the desired response based on the request. It can be HTML, XML or JSON. I'd suggest to use JSON for this since that's the most easiest produceable in Java and consumeable in JavaScript. You can use for example Google Gson to convert from a fullworthy Java object to a JSON string (and vice versa). E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOexception {
    // Populate response data somehow. Can be a String, Javabean or Collection/Map of either.
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("success", true);
    data.put("message", "Hello World!");
    data.put("param", request.getParameter("foo"));
     
    // Write response data as JSON.
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(data));
}

该servlet完成后,只需按通常的方式以web.xml对其进行映射即可.例如.在/firstServleturl-pattern上.

Once the servlet is finished, just map it in web.xml the usual way. E.g. on an url-pattern of /firstServlet.

然后,在jQuery中,您可以使用 $.getJSON() 从给定资源.第一个参数是URL,显然是firstServlet.第二个参数是回调函数,您可以在其中处理返回的响应数据.我已经出于纯演示目的传递了请求参数foo,这不是强制性的.

Then, in jQuery you can use use $.getJSON() to obtain JSON from the given resource. The first argument is the URL, which is obviously firstServlet. The second argument is the callback function wherein you can work on the returned response data. I've passed the request parameter foo for pure demonstration purposes, this is not mandatory.

$.getJSON('firstServlet?foo=bar', function(data) {
    alert('Success: ' + data.success + '\n'
        + 'Message: ' + data.message + '\n'
        + 'Param: ' + data.param);
});

当然,您不仅可以显示简单的警报,还可以做更多的事情.例如.根据返回的数据来操作/遍历当前页面中的HTML DOM.

You can of course do more with this than just displaying a simple alert. E.g. manupulating/traversing the HTML DOM in the current page based on the returned data.

在此之前,我已经发布了两个带有实际示例的答案,您可能会发现它也很有用:

I've posted two answers with practical examples before here, you may find it useful as well:

  • calling a java servlet from javascript
  • How to generate dynamic drop down lists using jQuery and jsp?

这篇关于使用jQuery Ajax从JSP页面调用Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆