Had an interesting problem float past me the other day, that had me thinking a little outside the CF square.
Situation: We have an existing HTML website, and we are moving it to a CF website.
Problem: We don't want to break any of our existing HTML links. I.e. if you we have http://www.mysite.com/page.html it should stay the same – so no sneaky .cfm extensions.
Interesting Point: It's a standalone CF Instance running on a J2EE server. Hmnnn…
So the obvious starting point is to map the CF Servlet to *.html – much like you could already do within a IIS configuration, nothing all that interesting there.
Obviously there is now some new CF magick, that allows for the pulling in of content (basically through a single CF Custom tag, that does some processing dependent on the URL).
But given the standard CF setup, that would mean we have to create a CF page for every old html page. That is seriously gonna suck, and is a lot of hard work, for not much payoff.
So of course, I got a' thinking and said – 'wait a minute, why can't we use a servlet to fake the html page, and then pass the URL information to a single CF page via the request scope?'
I.e. do something like this:
public class CFForward extends HttpServlet
private static final String FORWARD = "forward.cfm";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//grabs the servlet path
String servletPath = request.getServletPath();
Sets the request scope parameter called 'servletpath' that is accessable from the
Coldfusion page. Key must be LOWERCASE!!!
//forward on to the coldfusion page.
Then add this Servlet mapping to *.html (and get rid of the CF one) – and presto, seamlessly drives all *.html requests over to "forward.cfm", along with the relevent details in it's request scope.
That AND all the URL and FORM scope data is still there as you would need it.
(Note: my version pulls in details from the web.xml, and does some other stuff but I figured I'd keep it simple)
So now you have what looks like a static HTML page, which is really a servlet running a ColdFusion page behind the scenes.
Yup, I don't believe what a URL tells me anymore. Where have the days gone when .html was static, .cgi was perl, and .exe was crazy ;o) You always knew where you stood with a webpage, just by looking at it's extension. Now it's all smoke and mirrors.
The only problem I've hit so far – on the website, the web stats package picks up 404 errors, however, with a servlet mapped to *.html, ANY page with ends with a .html will get picked up by the servlet. If relevent (i.e. no content for that URL), I need to be able to push a 404 error to the Server (getting it to the client isn't hard, you don't even have to use a real 404). Not sure how I'm going to do that yet.
Thought it was a nifty idea just the same.