How to test your Ajax call using JSP

If you building the web app and want to test your Ajax call by calling the controller here is the snippet you can use.

$(document).ready(function() {
$.ajax({
type: "GET", 
url: "/YourControllerURL"
}).done(function(data) {
console.log(data);
});
});
  1. place the snippet inside the tag inside JSP
  2. in chrome press F12 for developer tools in chrome.
  3. navigate to console tab
  4. in browser navigate to JSP with snippet code.

 

Alternatively, you can view the navigation tab inside the developer tools and see your ajax request and response from controller.

Sweetness of this approach compared to Postman, is that you don’t need to provide all the authorization details.

Note: for testing purpose change your controller to GET method

Note 2: great article to learn about Ajax methods:

https://learn.jquery.com/ajax/jquery-ajax-methods/

Also if you need to pass data to your controller you can use bellow snippet. It returns json, of course your controller must return json too.


var data = {some data you need for your controller ex. id}
$(document).ready(function() {
$.ajax({
type: "GET", 
url: "/YourControllerURL",
contentType: "application/json",
dataType: "json",
data:data
}).done(function (nameOfJsonControllerReturns){ 

console.log(nameOfJsonControllerReturns.data); 

}); 
}); 

Action time: Let’s start some project

Dear readers and me, I’ve decided that I should start developing my own application. Only this action can justify my accumulation of knowledge. Therefore, let’s decide on couple things:

  1. Frontend: jQuery, AJAX and JavaScript
  2. Backend: Spring framework

As for app idea, I’ve decided that I will be making clone of project which I’m working on now. But I won’t reuse any materials from it, only the knowledge I’ve acquired. I want my project to be opensource so all the codes will be available on my GitHub account. The link I will put once I got anything to show.

Thank you.

Interaction between View and Controller aka Flow

When sending a request to your application the following happens:

Note: some steps may not be necessarily required

  • The request arrives at your server (e.g. Tomcat). Depending on the context path in the url the server decides to which application the request belongs.
  • Depending on the url and the servlet mapping in the web.xml file of your application the server knows which servlet should handle the request.
  • The request is passed to the servlet filter chain which can modify or reject requests
  • The servlet takes control over the request. In case of your Spring application the spring Dispatcherservlet receives the request. Now Spring kicks in
  • The request is processed by mvc intercepters preHandle methods
  • The request is mapped to a controller based on the url. The corresponding controller method will be called.
  • Your controller is processing the request. Many different responses can be returned in controllers (jsp, pdf, json, redirects, etc.). For now i assume you want to render a simple jsp view. Result of the controller are two things: a model and a view. The model is a map that contains the data you want to access later in your view. The view at this stage is most of the time a simple string containing a view name.
  • Registered springs mvc interceptors can kick in again using the postHandle method (e.g. for modifying the model)
  • The ‘view’ result of your controller is resolved to a real View using a ViewResolver. Depending on the ViewResolver the result can be jsp page, a tiles view, a thymeleaf template or many other ‘Views’. In your case the ViewResolver resolves a view name (e.g. ‘myPage’) to a jsp file (e.g. /WEB-INF/jsp/myPage.jsp)
  • The view is rendered using the model data returned by your controller
  • The response with the rendered view will be passed to mvc interceptors again (afterCompletion method)
  • The response leaves the dispatcher servlet. Here ends spring land.
  • The response passes servlet filters again
  • The response is send back to client