Reputation: 2048
Struggling on this one 2nd day. I have a select-option form with list of tags. I want to display articles from database depending on chosen tag. I have a div "showArticles", in my index.jsp, where I want to show articles. I am using jquery and ajax for that purpose. I wrote Servlet called test where I just output a simple string, but I cant even receive the text, seems that my servlet is never called. servlet is in package called "servlets", I am using NetBeans.
This is the 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 is the 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 is the 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 your page here. You may use following sample code. */
//response.getWriter().write("Omething");
} finally {
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. I am writing output in doPost(), I tried to write in in doGet and processRequest, but no luck.
Anyone have any idea why this does not work?
$.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);
}
}
Upvotes: 2
Views: 6924
Reputation: 2048
Problem is resolved. I ve changed the code as 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);
}
}
Upvotes: 1
Reputation: 1684
Try change your url to /test:
$.ajax({
type: "POST",
url: "/test",
data :"value="+option_value,
datatype : "html",
success: function(html) {
$("#showArticles").html(html);
}
});
Upvotes: 0