7

How to send json output from servlet to jsp?

 2 years ago
source link: https://www.codesd.com/item/how-to-send-json-output-from-servlet-to-jsp.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to send json output from servlet to jsp?

advertisements

I'm making an inventory control. I was trying to check if the entered quantity of item is less than the stock quantity or not. I'm getting the servet json output. But I can't send it to jsp back.

Jsp JQuery code.

<script>
        $('document').ready(function() {
            $('#submit_btn').click((function() {
                var $name = $("select#item_name").val();
                var $qty = $("input#qty").val();

                $.post('BuyItem', {item_name: $name, item_qty: $qty}, function(data) {
                    if (data !== null) {
                        alert(text(data));
                        $("input#qty").val("");
                    } else {
                        alert("Invalid Item!");
                    }
                }
                );
            }));
        });
    </script>

And this is servlet query.

   while (rs.next()) {

                if (rs.getInt("qty") > qty) {
                    int id = rs.getInt("item_id");
                    Gson gson = new Gson();
                    String json = gson.toJson(id);
                   // System.out.println("one" + json);
                    response.setContentType("application/json");
                    //            response.setCharacterEncoding("UTF-8");
                    response.getWriter().print(json);
                } else {
                    Gson gson = new Gson();
                    String json = gson.toJson("Stock doesn\'t have enough item quantity.");
                  //  System.out.println("two" + json);
                    response.setContentType("application/json");
                    //          response.setCharacterEncoding("UTF-8");
                    response.getWriter().print(json);
                }
            }

From both System.out.println() s output is always coming correctly. But not sending to jsp back. Please help me with this.


what you have to do and also a best practice is to create a DTO class with the properties that you need to pass to gson library.

    public class ResponseDTO implements Serializable{
          private Integer id;
          //more properties ...

          public Integer getId() {
            return id;
          }
          public void setId(Integer id) {
              this.id= id;
          }  

           // other getters & setters
   }

and inside your loop, set the values to the dto object, then pass it to gson.

Gson gson = new Gson();
          ResponseDTO dto = null;
          String json = "";
          response.setContentType("application/json");
          ......
          if (rs.getInt("qty") > qty) {
                dto = new ResponseDTO();
                int id = rs.getInt("item_id");
                dto.setId(id);
                ......

                json = gson.toJson(dto);
            } else {
               ...... // similar
                json = gson.toJson("{data: 'Some message'}");
            }
        response.getWriter().print(json);

gson will give you the proper json structure to the client side. try and see !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK