Hi,

I was trying to create a Filter that generates the some content as files (if
required) into the file system and lets the default servlet handle the rest.
However I noticed that if the uri was such that TC would interpret it as
"static content" like /foobar.html - I would get a 404 on the first request.
If the uri refers to more dynamic content such as /foobar.jsp - you don't
get a 404.

Assuming this is because of TC:s static content caching I tried to disable
it by setting context parameters cacheTTL to zero and cachingAllowed to
false. This worked with static uri's like /foobar.html but not with
/foobar.jsp or /foobar/.

Another interesting feature seemed to be that if the uri refers to a
"directory" like /foobar/ and eventhough the "dynamic" index.jsp has been
generated by the filter, 404 is still the result on the first request.

So the question is: how to force TC to check if file exists in real time?

Below a test Filter do demonstrate. Try uris like:

/somepage.jsp -> loaded on first request but cached
/somepage.html -> not loaded on first request (unless you have disabled
static caching from context)
/somedirectory/ -> not loaded on first request
/somenewdirectory/?forward=1 -> loaded on first request


-jukka-


ps. tested on TC 6.0.14.


---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<---8<


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author jsr
 */
public class FilterThatGeneratesContent implements Filter {


    private FilterConfig filterConfig = null;


    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();

        // assumes not deployed as ROOT
        String relativeURI = uri.substring(uri.indexOf("/", 1),
uri.length());

        if (relativeURI.equals("/")) {
            relativeURI = "index.jsp";
        } else if (relativeURI.endsWith("/")) {
            relativeURI += "index.jsp";
        }

        try {

            createFile(relativeURI);

            if (request.getParameter("include") != null) {
                httpRequest.getRequestDispatcher(relativeURI).
                        include(request, response);
            } else if (request.getParameter("forward") != null) {
                httpRequest.getRequestDispatcher(relativeURI).
                        forward(request, response);
            } else {
                chain.doFilter(request, response);
            }


        } catch (Exception e) {
            throw new ServletException(e);
        }



    }


    private void createFile(String relativeURI) throws Exception {

        String webAppDir = filterConfig.getServletContext().getRealPath("");

        File contentFile = new File(webAppDir, relativeURI);

        contentFile.getParentFile().mkdirs();

        FileWriter writer = new FileWriter(contentFile);

        writer.write("<html><body>" +
                "<pre>" +
                "Current Servlet time: " + new Date() +
                "<br/>" +
                "Current JSP time:     <%=new java.util.Date()%><br/>" +
                "</pre>");

        writer.flush();
        writer.close();

    }


    public void destroy() {
    }


    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;

    }


}

Reply via email to