1-by-1 black pixel for creating lines
1-by-1 black pixel for creating lines
EricGiguere.com > About This Site > How This Site Works
Printer-friendly version Set your preferences
Read my blogs on AdSense and affiliate marketing
 
 
  
Learn about these ads

How This Site Works

As mentioned on the preferences page, this site is completely dynamic: all HTML markup is dynamically generated. This lets me do things like track session preferences without requiring cookie support. For those who are curious, this page explains how things are done.

EricGiguere.com is Java-powered

For many years now I've been programming mostly in Java. I'm very comfortable with the language. I had a static website before, but I really wanted to build a dynamic one, and Java was the natural choice for me. It may not be your choice, but it works well for me.

Of course, building a Java-powered site is a lot easier these days. Java servlets and JavaServer Pages (JSP) provide the foundation. The JSP Standard Tag Library (JSTL) provides a powerful set of tags for doing page and data manipulation. Open-source tools like Tomcat and Ant make it easy to build and test web applications based on servlet and JSP technology.

Really, the hardest thing about building a Java-powered site these days is finding a place to host it. If you have the bandwidth and your ISP lets you, you can always host it yourself using your own Tomcat-based server, of course. If this is not an option, though, you need to find a web hosting service that supports servlet and JSP technology. This site is being hosted by KGB Internet Solutions, but there are other providers out there, just not as many as those offering Perl, PHP or ASP technologies.

So Where Are The JSPs?

You may think I'm lying to you when I say that this site is Java-powered, because all the URLs for this site end in the ".html" suffix we're so familiar with instead of the ".jsp" suffix you'd normally expect. The answer is simple: the pages are generating HTML, so they end in ".html". Most users of this site don't care one way or another how the pages are generated. You do, because you're reading this page, but if I hadn't told you the site was Java-powered you probably wouldn't have figured it out.

How does it work? It's not that hard! EricGiguere.com is one big web application (webapp). The web.xml file that defines the webapp includes this section:

<filter>
<filter-name>HTMLFilter</filter-name>
<filter-class>com.ericgiguere.servlet.HTMLFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>HTMLFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>

In other words, any request for a page ending in ".html" is intercepted by a servlet filter. The filter code is very simple:

package com.ericgiguere.servlet;

import java.io.IOException;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HTMLFilter implements Filter {
    public HTMLFilter() {
    }

    public void doFilter( ServletRequest req, 
                          ServletResponse res,
                          FilterChain chain ) 
                              throws ServletException, 
                                     IOException {
        HttpServletRequest  hreq = (HttpServletRequest) req;
        HttpServletResponse hres = (HttpServletResponse) res;
        ServletContext      context = _config.getServletContext();
        boolean             doChain = true;
        String              path = hreq.getServletPath();

        req.setAttribute( "path", path );

        if( path != null && path.endsWith( ".html" ) ){
            String jsp = path.substring( 0, path.length() - 4 ) 
                            + "jsp";

            if( context.getResource( jsp ) != null ){
                ServletHelper.forward( hreq, hres, jsp );
                doChain = false;
            }
        }

        if( doChain ){
            chain.doFilter( req, res );
        }
    }

    public void init( FilterConfig config ) {
        _config = config;
    }

    public void destroy() {
    }

    private FilterConfig _config;
}

In case you're wondering, ServletHelper.forward is trivial:

public static void forward( HttpServletRequest req,
                            HttpServletResponse res,
                            String path )
                            throws ServletException, 
                                   IOException {
    RequestDispatcher rd = req.getRequestDispatcher( path );
    rd.forward( req, res );
}

So basically:

  1. a request comes into the filter for a given page.
  2. the filter replaces the ".html" extension with a ".jsp" extension
  3. if the given JSP exists, the filter forwards the request to it
  4. otherwise the filter serves up the originally-requested page

The filter also does a few other things not shown here, but they're not important. Once the request is forwarded to a JSP, everything is pretty standard from then on.

To be truly safe, you should also make sure that no JSP can be accessed directly from a browser. You can do this using the security-constraint section of the web.xml file.

The final trick is to place a dummy index.html file in each directory of the webapp, otherwise the filter mapping for the welcome file (which is defined as index.html but of course is really generated by index.jsp) won't work.

Google Web www.ericgiguere.com   
1-by-1 black pixel for creating lines
 
Copyright ©2003-2012 Eric Giguere | Send mail about this page | About this site | Privacy policy
Site design and programming by Eric Giguere | Hosting by KGB Internet Solutions
This site is Java-powered
Other sites: The Unofficial AdSense Blog | Google Suggest Explorer | Invisible Fence Guide | Synclastic
This page was last modified on September 3, 2003
1-by-1 black pixel for creating lines
1-by-1 black pixel for creating lines