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.
This code returns my response as null.
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.
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<Room> 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");
}
And the ajax from my JSP:
$(function(){
$("#hotelSelector li").click(function(){
var hid = $(this).attr('id');
$.ajax({ type: "GET",
url: "AppController?action=selectedhotel&hotel_id=1",
success : function(text)
{
alert(text);
// This will show the values. Change "alert" for $('div#mydiv').html(value) or so
}
});
});
});</script>
Riiight...so , please fix ?