Javascript – JSP in Javascript file or multiple AJAX requests

javascriptjsppatterns-and-practices

I've been thinking about how I want to load my data for some checkboxes in an AngularJS app. I'm generating multiple checkboxes based on 2 JSON objects that are currenty hard-coded in the a Javascript file (it's currently a prototype, but in production the values for the checkboxes can change from day to day).

Now I've tried using JSP in my Javascript file (IDE is Eclipse) – while it would work, it just looks ugly (besides the fact that JSP syntax checking doesn't work in a .js file). I thought about creating a servlet that would generate those JSON objects and then loading them through AJAX, but I'm not sure if this might be "bad behaviour" since I'd generate multiple AJAX calls only to keep my code "clean".

Is there some kind of convention or best-practice for dynamically created Javascript or should I usually prefer AJAX over JSP in Javascript?

Best Answer

What you want to do is possible and there are available in many frameworks that can help here. Look at AJAX as a callback implemented in JS and not something different - this will help simplify your problem. There is no best practice IMO, but there are some options available that I have used in the past. Here are some options,

  1. JSP Tag UI Components: You have some widget which requires some business input to render but is mostly JavaScript. While AJAX is an option it can be unnecessary if the data is available at the point of rendering the JSP. One approach is to create a JSP Tag Lib that internally renders the HTML and JS. The benefit is fewer people are touching and working with JS which is more error prone and relatively difficult to Unit Test. In the past this was a messy option since you would have to use multiple out.println statements to render the Dynamic JS from the Tag Handler but now with the introduction of Tag Files this can be a lot cleaner.

  2. JS Endpoint: JS Endpoints are special servlets that serve dynamic JS. Think of this instead you setting a static file in the src attribute of the script tag you have the URL pointing to a URL pointing to a Servlet that responds with content type for JavaScript or JSON as the case may be.

    <script src="/myapp/abc.jsx"></script>
    

    In the web.xml,

    <servlet-mapping>
    <servlet-name>myServletThatServesDynamicJS</servlet-name>
    <url-pattern>*.jsx</url-pattern>
    </servlet-mapping>
    
  3. Client-side Frameworks: There are many JS frameworks worth your time investment that can minimize the amount of JS you write by declaratively generate a lot of JS (and CSS) something that most Java developers would prefer. AngularJS and Bootstrap are examples. So you basically add some additional attributes to regular HTML tags and there is some magic in the background that generates components, makes AJAX calls, etc. Angular will pull required JS "modules" from the server as an when needed via AJAX calls and also supports advance features like DI which can make testing your JS code a breeze and also a lot cleaner.

I personally prefer using JS frameworks to solve JS problems. So I would be in favor of option 3 for most of the issues. JS frameworks have come a long way and have wide industry support especially when you are talking of Frameworks like AngularJS or even jQuery. They also address many other concerns some that you haven't raised like cross browser compatibility which can be a huge pain even if you are supporting just one browser across versions.