jQuery ajax不会调用Servlet

时间:2013-04-05 14:38:21

标签: jquery ajax jsp servlets

第二天在这一天苦苦挣扎。 我有一个带有标签列表的选择选项表单。 我想根据所选标签显示数据库中的文章。我在我的index.jsp中有一个div“showArticles”,我想在那里展示文章。 我正在为此目的使用jquery和ajax。 我写了一个名为test的Servlet,我只输出一个简单的字符串,但我甚至无法收到文本,似乎我的servlet从未被调用过。 servlet在名为“servlets”的包中,我正在使用NetBeans。

这是表格:      

                            <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>

这是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);
                   }
           });
});

});

这是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>
}

我试过了      PrintWriter out = response.getWriter();      通过out.println( “东西”);

同样没什么。 我在doPost()中编写输出,我试着写入doGet和processRequest,但没有运气。

任何人都知道为什么这不起作用?


问题已解决。我更改了代码如下:

 $.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);
    }
}

2 个答案:

答案 0 :(得分:1)

问题已解决。我更改了代码如下:

$。AJAX({         网址:“测试”,         类型:'GET',          * data:{value:option_value},*

    error : function(jqXHR, textStatus, errorThrown) {
        alert(textStatus);
    },
    success : function(html){
       $("#showArticles").html(html);
       }
    }
});

的Servlet

@覆盖 protected void doGet(HttpServletRequest请求,HttpServletResponse响应)         抛出ServletException,IOException {     试试{

    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);
}

}

答案 1 :(得分:0)

尝试将您的网址更改为/ test:

$.ajax({
                   type: "POST",
                   url: "/test",
                   data :"value="+option_value,
                   datatype : "html",
                   success: function(html) {
                               $("#showArticles").html(html);
                   }
});