Communication between JSP and Servlet?

后端 未结 1 1108
轻奢々
轻奢々 2021-01-21 02:23

I have a jsp page that communicate with a servlet back end. Up until now, the way I communicate with that servlet is via .getJSON() which is a JQuery method. This work great if

相关标签:
1条回答
  • 2021-01-21 03:15

    Just have the data in a collection or map of fullworthy Javabeans and make use of Google Gson to convert it to JSON without any pains. JSON is more compact than XML and much easier to process in JavaScript (it's also the JavaScript Object Notation).

    All you basically need to do with help of Gson is the following:

    List<Data> list = dataDAO.list();
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(list));
    

    That's all. I've answered this several times before with examples: here, here, here, here and here.

    0 讨论(0)
提交回复
热议问题