title: Getting data from servlet to ajax?
body: <p>Jsp / servlets seems to be much more tedious than I'd have expected it. I'm trying to call a servlet function through ajax and actually have it feed some data back to my front end , which is a jsp file.</p>
  
  <p>This code returns my response as null.</p>
  
  <p>This is part of my servlet. I'm trying(desperately as is fairly obvious from the code) to have it send something - anything back to ajax.</p>
  
  <pre><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String action = (String) request.getParameter("action");
  if (action.equalsIgnoreCase("selectedhotel"))
      {
  
          response.setContentType("text/plain");  
          response.setCharacterEncoding("UTF-8"); 
          System.out.println("test");
          String attribute = (String) request.getParameter("hotel_id");
          System.out.println(attribute);
          List&lt;Room&gt; aRooms;
          aRooms = model.getRoomByHotel(Integer.valueOf(attribute));
          request.setAttribute("aRooms", aRooms);
          request.setAttribute("list", list);
          PrintWriter outPrintWriter = response.getWriter();
          outPrintWriter.write("ASDSADA");
              outPrintWriter.println("test");
      }   
  </code></pre>
  
  <p>And the ajax from my JSP: </p>
  
  <pre><code>$(function(){
  $("#hotelSelector li").click(function(){        
      var hid = $(this).attr('id');
  
      $.ajax({ type: "GET",   
           url: "AppController?action=selectedhotel&amp;hotel_id=1",   
           success : function(text)
           {
              alert(text);
               // This will show the values. Change "alert" for $('div#mydiv').html(value) or so
           }
      });
  });
  });&lt;/script&gt;
  </code></pre>
  
  <p>Riiight...so , please fix ? </p>