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?