There's loads and loads and loads of ways of doing that.
I find it's easiest though if you use an (either client-side or server-side) framework that has a template engine. In Apache
Struts 2, the default template language is FreeMarker, but it also supports Velocity and
JSP. You can also install plugins that let you use other template engines, such as Thymeleaf.
Have you ever worked with a template language like FreeMarker, Velocity, JSP or Thymeleaf?