0

I'm new to use json and ajax. Please someone help me on how to append the values to the option tag. The values are getting printed on the page itself than inside the option tag. I have seen so many examples but do not understand.

Retrieveservlet :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   // TODO Auto-generated method stub

   System.out.println("action is called inside dropdown");
   ViewValuesInDB daoobj = new ViewValuesInDB();
   ArrayList<String> list = new ArrayList<String>();
   String json = null;
   try {
       list = daoobj.makeConnection();
       System.out.println(list);
       if(null == list){
           System.out.println("list is null");
       }
       else{
           System.out.println("list has values");
       }
       System.out.println("size of ids list :"+list.size());        

       /*using json*/
       json = new Gson().toJson(list);
       System.out.println(json);

       response.setContentType("application/json");
       response.getWriter().write(json);

    }catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}

my jsp code :

<script>
    $(document).ready(function() {
        $.get("Retrieveservlet", function(responseJson) {   
            ('#dropDownId').append($('<option>').text(value)('value'));
        });
    });
</script>
<body>
    <label for="dropDownId">Country*:</label>
    <select id="dropDownId">
    </select>
</body>
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Rebecca
  • 25
  • 2
  • 8
  • I suggest you to read this question - https://stackoverflow.com/questions/3608891/pass-variables-from-servlet-to-jsp – fabfas Jun 08 '17 at 12:24
  • Hi fabfas.Thanks for the link. i red the link. I have already implemented this using jstl and using setattribute in servlet. Im trying to implement now using json. Any ways to implement this in json? – Rebecca Jun 08 '17 at 12:40
  • @Rebecca Servlet - JSP connection doesn't work like that. The JSP is pre-compiled into to Java and can access Java objects passed to it. It all happens server side. – Jaydee Jun 08 '17 at 13:48
  • Hi jaydee. I want to try populating dependent drop-down also. All the values must retrieve from database. So I thought using Json, Ajax will be the best. So what can I do if I want to populate depended drop-down without using Json? Any suggestions? – Rebecca Jun 08 '17 at 14:32

1 Answers1

0

This code works and appending in the dropdown but the values are getting displayed at the top of the page too.Can someone give me solution for this?

    <script>
        $(document).ready(function() {
            $.get("Retrieveservlet", function(json) {
                /*  x = json[0];
                 alert(x); */
                var myselect = $('#dropDownId');
                $.each(json, function(index, key) {
                    myselect.append($('<option></option>').val(key).html(key));
                });
                //$('#dropDownId').append(myselect.html());
            });
        });
    </script>
Rebecca
  • 25
  • 2
  • 8