Search code examples
javaservletsweb.xmlurl-mapping

How to make servlet map a special url to a static resource?


I know, by the default servlet, static resources will be returned automatically. However, my url is quite special and I don't want it looks like http://mysite/app/test.html. Maybe it is like that:

http://mysite/app/dosomething/7419698f

I want to map(or forward?) this request to a static html file, for example /WEB-INF/pages/dowork.html. For more, 7419698f is only a parameter, http://mysite/app/dosomething/2926739e will also be mapped to the same static file. I know a workaround like that:

<servlet>
    <servlet-name>test</servlet-name>
    <jsp-file>/pages/dowork.html</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/dosomething/*</url-pattern>
</servlet-mapping>

It works only if the dowork.html is a legal jsp file. If I want to serve image resource like that, it would turn error.


Solution

  • Leos Literak's clue is right. My requirement cannot be done by simple xml config, I must do something more.

    I need a filter. In web.xml:

    <filter>
        <filter-name>summary-fw-filter</filter-name>
        <filter-class>com.mycompany.mywebapp.filter.SummaryForwardFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>summary-fw-filter</filter-name>
        <url-pattern>/summary/*</url-pattern>
    </filter-mapping>
    

    The code of the filter:

    public class SummaryForwardFilter implements Filter{
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest)servletRequest;
            HttpServletResponse response = (HttpServletResponse)servletResponse;
            String url = request.getRequestURL().toString();
            String[] urlSegments = url.split("/");
            String guid = urlSegments[urlSegments.length-1];
            if(guid.matches("^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$")){
                request.getRequestDispatcher("/WEB-INF/pages/summary.jsp").forward(servletRequest, servletResponse);
                return;
            }
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    
        @Override
        public void destroy() {
        }
    }
    

    Then, the url http://mysite/mywebapp/summary/ecef22d6-7aa6-49db-b0d3-6577a63d14c8 will be mapped to the /WEB-INF/pages/summary.jsp. It's also okay when trying to map to non-jsp files.

    The guid parameter can be retrieve by javascript code like that:

    function extractGuid(value) {
        var re = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
        var match = re.exec(value);
        return match ? match[0] : null;
    }
    
    var guid = extractGuid(window.location.href);