Putting Java into HTML is a solution for two issues:
1 Not all HTML page designers know Java
App developers know Java. Web page designers know HTML. With JSP, Java developers can do Java, and HTML developers can do web pages.
2 Formatting HTML into a String literal is REALLY ugly
Putting even marginally complex HTML into the argument to a println() is a compiler error waiting to happen. You might have to do a ton of work to get the HTML formatted properly in a way that still works in the client’s browser, yet satisfies Java rules for what’s allowed in a String literal. You can’t have carriage returns, for example, yet most of the HTML you’ll pull from a web page editor will have real carriage returns in the source. Quotes can be a problem too—a lot of HTML tags use quotes around attribute values, for example. And you know what happens when the compiler sees a double quote... it thinks, “This must be the end of the String literal.” Sure, you can go back and replace each of your double quotes with escape codes... but it all gets insanely error prone.
Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.