Thursday, July 1, 2010

A JSP limitation: You can't share JSP files between web applications

This one has been a real frustration of mine:  Say you have a couple of Java Servlet/JSP web applications.  Each of these applications shares common jsp files.  For example, a login.jsp page, header.jsp and footer.jsp, etc.  There is no "easy" way to package these common jsps into a jar and have each web application share these jsps.

There are a couple of workarounds:
  1. Manually convert the jsp into a servlet.  This one is a hard one, and it results into a lot of request.getWriter().print() statements into your servlet.
  2. Pre-compile the jsp into a servlet, and then package the servlet into a jar.  Then map the servlet into the web.xml file.
With method 1, it is easy to make changes.  With method 2, it is hard since the most jsp compilers produce difficult to manage code.