Java Web Development – When Are Scripts Inside HTML Not Considered Bad Practice

htmljavajspscriptingweb-development

I am new to web development. While studying the Play framework and JSP, I noticed that they both give an option to place script (in jsp scriptlet) inside HTML, but in both books I am reading they both say it is bad practice and don't give an explanation as to when should I use scripts, only writing not to.

My question is, if both frameworks supply such an option, when would it be ok to use scripts inside HTML?

Please note, I'm talking about server side scripts for creating dynamic HTML (i.e. JSP scriptlet).

Best Answer

There are mostly two reasons to put scripts outside HTML code:

  1. Server-side, separation of concerns and clean code. You quickly understand why you must be very careful with that when you see some PHP code which, in the same piece of code, mixes PHP, SQL queries, JavaScript, HTML, CSS and Base64-encoded images embedded in CSS.

  2. Client-side, external scripts are easily cached. If your HTML is a dynamic page, it may change often. By putting scripts outside HTML, you allow your visitors to download this content once, and never refresh it again (well, not never, but rarely). In the same way, if two static or dynamic HTML pages share the same script, putting it in an external file will allow to download it once.

The first point is valid in all cases. You'll never be able to justify that it's cleaner to mix everything in one file. Why frameworks allow this? Because there are situations where you don't care about clean code:

  • If I write a small page I'll throw away for sure, and I must do it quickly, it's not so bad to put everything in one file.

  • If I want to show a short example to my colleague (by putting a single file to a document management system platform) or publish it to a blog¹, having everything in one file can be acceptable and useful too.

  • If I must create a small prototype before developing the application for a customer, best practices don't matter at all, while reducing cost is critical for source code which will be thrown in all cases.

The second point, on the other hand, explains why there are perfectly valid cases to put scripts in HTML:

  • The script changes on every change of a dynamic page. In this case, why would you force the client to download the HTML file, then the separate file containing the script (i.e. doing two requests, which is a waste of time and resources which must not be neglected for the websites of some scale), while you can provide the same thing through one request?

  • The static HTML page uses the script which is not shared by any other page of the website. It's a perfect candidate too, if you want to avoid doing two requests².

Now, returning to the first problem (clean code), you can serve scripts in HTML and have clean code server-side (for example by using a template which will inject, at runtime, the script which is written separately). This, of course, requires some level of expertise, and as I said before, combining scripts and HTML is needed only on high-scale websites. This means that if you're seeing an inexperienced developer mixing scripts and HTML when doing a personal website, it's a good sign that something is wrong.


¹ Personally, I'm against this practice. More we share through our blogs code which does not follow best practices, more the less experienced developers would be encouraged to copy it and to believe that this is the right style.

² With appropriate caching configuration, two requests will happen only for users with empty cache. For others, you can set the expiration date for the far future, and the browser will avoid the request which will return the 304 Not Modified response.

Related Topic