samedi 3 janvier 2015

Pros and cons of storing HTML and JavaScript as Strings in Java for Web Page Generation


Generating HTML pages by retrieving HTML and JS from Strings in my java app is something I am thinking about. What are the pros and cons of doing so?


One of the projects I am working on has about 8000 lines of code in which various bits of HTML, JavaScript, and CSS are stored. Is it a good or a bad practice to store HTML, CSS and JavaScript in Java at this scale? I am not aware of the best practices used during web development for storing and retrieving HTML, JavaScript, and CSS templates.


The alternatives I can see here are these:



  1. Put HTML, CSS, and JavaScript in .html, .js, and .css files in different directories and use a library like jQuery to retrieve files, load them, and update the DOM accordingly.

  2. Do what I am doing by storing HTML, CSS, and JS in Java files as strings and build a page from a Java service.

  3. Store HTML, CSS, and JS in a database and pull each item out as needed for the client.


I am currently doing Alternative 2 and am considering Alternative 3 for parts of my application.


Here is what happens when a user requests a page from my app:



  • In a Tomcat servlet A ReST controller receives an HTTP GET request for a page.

  • The page is built from various HTML, CSS, and JavaScript components contained in strings in several Java classes of my page-generation service.

  • The page is returned to the user


Some of the pros I can think of are:



  1. I can use Java Objects to store and maintain code snippets and reuse the page components on multiple pages

  2. I can store templates, pieces of pages, and units of my front-end code in classes where it could make sense to group components.

  3. Easy to manage one template.


Some of the cons I can think of are:



  1. Debugging HTML and JS could be extremely difficult

  2. Adding and changing things could be very hard since all of the HTML, CSS, and JS for a given document isn't right there in front of me

  3. SEO would be more difficult or impossible since static resources are always generated, thus not indexable.


I am open to any ideas on the topic. I am finding it difficult to update and change things with storing JS and HTML in Java as strings so am considering pulling everything out of Java to be regular .html, .css, and .js files. I am hesitant to refactor 8000 lines of code though and it could be a nightmare trying to make everything work again outside of my Java code. Is 8000 lines of code much to refactor? I am not yet familiar with the scale of enterprise application development.


Edit: This is a repost of the question already posted here on SO. The question was put on hold and a moderator recommended I repost it here since it was more appropriate for this site.


If you think I could reword the question better let me know and I'll do my best.





Aucun commentaire:

Enregistrer un commentaire