0

I am creating calendar web app with maven, and I was trying to use JQuery .ajax to update the site without reloading the page. However I have problem with updating correct values.

Here is my doGet method from the servlet:

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

}

And here is .ajax in JSP:

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

I also tried with this, but the same effect:

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

});

I simplified the code to better show the problem. I am trying to decrement monthChanged value and send it to the site on button click. The thing is that System.out.println("monthChanged"); is printing decremented value, but when I try to console.log() it on the site, it shows the first value 10. I tried to do this in many ways, but I cannot find the solution. Is this second dispatcher in else block overriding the first one?

afternun
  • 3
  • 2

1 Answers1

0

You cannot get the value of a servlet attribute via an ajax request.

I highly recommend checking out this question on how to do ajax with servlets

In your servlet "unischedule", you need to write your monthChanged variable to the response. Like this:

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

}

Now in your jsp, you can retrieve the response like so:

 $(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           
            });
        });
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44