Okay, according to the project explanations here, it is definitely possible to decorate any web site backed by any server side technology (assuming the server has J2EE support too) using SiteMesh. Anyone reading those explanations for the first time may think of an off-the-shelf support, like you drop SiteMesh jar and your web.xml and it works! But how much is this true in real world especially when you want to apply such solution on a living website which is already in production not just a test "localhost" site? Don't get me wrong... SiteMesh itself is a great framework and I've promoted it everywhere I've started a new project but it's not that easy to apply this framework on other non-java server side technologies. Here is my story:
A few weeks ago, I moved my personal domain to a new hosting which supports J2EE too (from my previous PHP-only one). It had been a mess to maintain the look&feel of my site in a mix of static HTML and PHP environment, so I decided to do such decorations using SiteMesh. The framework I've used in every single J2EE web application I've developed in the recent years, but never got the chance to use it on my own site because of the lack of J2EE support.
Extracting the current look&feel of my site, designing a new navigation system and setting up SiteMesh to decorate my static HTML pages was just a matter of few hours of work. Very easy, straight forward and rock solid.
But as for my PHP pages I was a bit in trouble! The site was going to be backed by Apache2 for non-java stuff (as the master server) and Apache Tomcat for my j2ee applications (as slave), and the connection between these two was via JK_Module. Obviously Apache2 was serving the static contents (like images, CSS files and so) and PHP files. The rest were forwarded to Tomcat.
The first problem was here!! Tomcat (thus SiteMesh) didn't even get the PHP requests because Apache2 was directly catching the requests and serving them without forwarding. The result was that the PHP pages were not getting decorated although SiteMesh was present there and I've set its apply pattern on "/*" :-) Ok! It just took me an email to hosting support and they were kind enough to quickly modify the configuration files so that even *.php requests get forwarded to Tomcat (for my domain only) and Apache2 no more was in charge of processing them.
But wait, the second problem! Does Tomcat have any built-in PHP processing support? NO! So how's it going to do this? Well, poking around a bit (obviously googling), I found out that there is a pre-bundled Servlet with PHP Distributions which makes *.php request processing possible. It's actually a proxy Servlet which you map *.php requests on it, and it processes them using the command line PHP interpreter. So far so good! Again I contacted the support to make PhpServlet jar file available in Tomcat's common/lib folder. It was done within some minutes and in the meanwhile I'd modified my web.xml to include PhpServlet configuration block.
After a couple of hours of debugging because of an output flushing issue (that caused SiteMesh not to get the input from PhpServlet when the volume of input was not hitting the minimum threshold to be flushed), the server was ready to serve and to decorate the PHP files. Yes, finally got it working and started to give it a more thorough test.
Hey wait! Why does my site go completely down after each time I try to access an invalid/unavailable PHP page? Oops! Seems that Tomcat crashes when it gets a "404 Page Not Found" error from PhpServlet. Googling again showed me that this is a common problem and many have reported this issue but there is still no solution. Well, end of the story :-) Fortunately my PHP codes were not a lot or complicated, and I could easily translate them into JSP and you know the rest.
Just three final thoughts which I found them the real hurdles:
Armond