【问题标题】:Getting incorrect values from servlet to JQuery .ajax method从 servlet 获取不正确的值到 JQuery .ajax 方法
【发布时间】:2018-04-02 19:11:11
【问题描述】:

我正在使用 maven 创建日历 Web 应用程序,并且我试图使用 JQuery .ajax 来更新站点而不重新加载页面。但是我在更新正确值时遇到问题。

这是我的 servlet 中的 doGet 方法:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

    int monthChanged = 10;

    String action = req.getParameter("action"); 
    String jsp = "/jsp/unischeduleshow.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jsp);

    if(action != null){
        monthChanged--;
        req.setAttribute("monthChanged", monthChanged);
        dispatcher.forward(req, resp);
        System.out.println(monthChanged);
    }
    else{
        req.setAttribute("monthChanged", monthChanged);
        dispatcher.forward(req, resp);
    }

}

这里是 JSP 中的 .ajax:

 $.ajax({
type: "GET",
data : { action: "backMonth"},
url : "/unischedule",
success: function(){
    console.log("${monthChanged}");
}

我也试过这个,但效果一样:

$(document).ready(function(){          
      $(document).on("click", "#forward", function() {
            $.get("/unischedule",{action:"backMonth"}, function(responseText) {
                console.log("${monthChanged}");
            });
       });

});

我简化了代码以更好地显示问题。我正在尝试减少monthChanged 值并在单击按钮时将其发送到站点。问题是System.out.println("monthChanged"); 正在打印递减的值,但是当我在网站上尝试console.log() 它时,它显示第一个值10。我试图以多种方式做到这一点,但我找不到解决方案。 else 块中的第二个调度程序是否覆盖第一个调度程序?

【问题讨论】:

    标签: jquery ajax jsp servlets


    【解决方案1】:

    您无法通过 ajax 请求获取 servlet 属性的值。

    我强烈建议在 how to do ajax with servlets 上查看这个问题

    在您的 servlet“unischedule”中,您需要将您的 monthChanged 变量写入响应。像这样:

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    
    int monthChanged = 10;
    
    String action = req.getParameter("action"); 
    String jsp = "/jsp/unischeduleshow.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jsp);
    
    if(action != null){
        monthChanged--;
       // req.setAttribute("monthChanged", monthChanged);
       // dispatcher.forward(req, resp);
        System.out.println(monthChanged);
    }
    else{
       // req.setAttribute("monthChanged", monthChanged);
      //  dispatcher.forward(req, resp);
    }
    
    response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
    response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
    response.getWriter().write(Integer.toString(monthChanged));       // Write response body.
    
    }
    

    现在在您的 jsp 中,您可以像这样检索响应:

     $(document).on("click", "#forward", function() { // When HTML DOM "click" event is invoked on element with ID "forward", execute the following function...
                $.get("../unischedule",{action:"backMonth"}, function(responseText) {   // Execute Ajax GET request on URL of "unischedule" and execute the following function with Ajax response text...
                     console.log(responseText);        // your monthChanged value           
                });
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多