jQuery ajax does not call Servlet -
struggling on 1 2nd day. have select-option form list of tags. want display articles database depending on chosen tag. have div "showarticles", in index.jsp, want show articles. using jquery , ajax purpose. wrote servlet called test output simple string, cant receive text, seems servlet never called. servlet in package called "servlets", using netbeans.
this form:
<select id="b_sub_tag" name="b_sub_tag"> <option value='${0}'>subject</option> <c:foreach var="item" items="${subtaglist}"> <option value='${item}'>${item}</option> <c:set var="i" value='${i+1}'> </c:set> </c:foreach> </select>
this jquery:
$(document).ready(function(){ $("#b_sub_tag").change(function(){ var option_value = $(this).children('option:selected').val(); $.ajax({ type: "post", url: "test", data :"value="+option_value, success: function(html) { $("#showarticles").html(html); } }); });
});
this servlet:
@webservlet(name = "test", urlpatterns = {"/test"}) public class test extends httpservlet { protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { /* todo output page here. may use following sample code. */ //response.getwriter().write("omething"); } { out.close(); } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); response.getwriter().write("smething"); } @override public string getservletinfo() { return "short description"; }// </editor-fold> }
ihave tried printwriter out = response.getwriter(); out.println("something");
as well, nothing. writing output in dopost(), tried write in in doget , processrequest, no luck.
anyone have idea why not work?
problem resolved. ve changed code follows:
$.ajax({ url : "test", type: 'get', ***data: {value:option_value},*** error : function(jqxhr, textstatus, errorthrown) { alert(textstatus); }, success : function(html){ $("#showarticles").html(html); } } });
servlet:
@override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try { final initialcontext context = new initialcontext(); statefulb = (statefulbeanremote) context.lookup("java:comp/env/statefulbeanremote"); response.setcontenttype("text/plain"); response.setcharacterencoding("utf-8"); response.getwriter().write("a text"); } catch (namingexception ex) { logger.getlogger(loadarticlesbytag.class.getname()).log(level.severe, null, ex); } }
problem resolved. ve changed code follows:
$.ajax({ url : "test", type: 'get', *data: {value:option_value},*
error : function(jqxhr, textstatus, errorthrown) { alert(textstatus); }, success : function(html){ $("#showarticles").html(html); } } });
servlet:
@override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { try {
final initialcontext context = new initialcontext(); statefulb = (statefulbeanremote) context.lookup("java:comp/env/statefulbeanremote"); response.setcontenttype("text/plain"); response.setcharacterencoding("utf-8"); response.getwriter().write("a text"); } catch (namingexception ex) { logger.getlogger(loadarticlesbytag.class.getname()).log(level.severe, null, ex); }
}
Comments
Post a Comment