0
votes

My goal is reaching the same servlet servlet1 with all urls of the following pattern:

myserver.com/path1/*

where * can be anything like "x", "x/xx", "x/xx/x.cfg" etc.
In the end I only want to treat urls following the regex [0-9a-zA-Z]+.(cfg|xml|htm) but I am fine with doing so in the servlet. The servlet parses the original URL and dynamically builds the requested config files.

When I have the following mapping

<servlet-name>servlet1</servlet-name>
<url-pattern>/path1</url-pattern>

as expected the url myserver.com/path1 leads to servlet1.
When I use the following mapping which looks like the one that makes sense to me:

<servlet-name>servlet1</servlet-name>
<url-pattern>/path1/*</url-pattern>

I can still reach servlet 1 with the url myserver.com/path1/.
I can not reach the servlet with the url myserver.com/path1/path2, this url actually throws me back to the welcome-files entry.

Actually, anything with a second path throws me back to the welcome-files entry regardless of what patterns I enter. With second path I mean anything myserver.com/path1/* where * is longer than 0 characters.

The only alternatives I currently see is letting the welcome-files entry handle my requests (ugly) or using

.cfg .xml *.htm

which makes urls like myserver.com/XXX.cfg work (myserver.com/X/XXX.cfg doesn't work). This would be ugly as well and would interfere with future development of that webapp though.

How do I get the mapping to work the way I want it to?

1
It looks like a mistake in your configuration file, because /path1/* should work like a charmAndremoniy
read here about URL patternsBraj
Andremoniy: Any idea where I could look? The web.xml of that application only has a few properly working <servlet> and <servlet-mapping> entries besides the welcome files.ASA

1 Answers

0
votes

Try with a Filter if this path /path1 is called most of the times and put your logic there.

You have full control on this filter. You can define skip URLs in web.xml separated by commas.

I used this Filter for compression logic.

web.xml:

<filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.x.y.z.servlet.MyFilter</filter-class>
    <init-param>
        <param-name>skipEqualsURIs</param-name>
        <param-value></param-value>
    </init-param>
    <init-param>
        <param-name>skipStartsWithURIs</param-name>
        <param-value></param-value>
    </init-param>   
    <init-param>
        <param-name>skipEndsWithURIs</param-name>
        <param-value></param-value>
    </init-param>   
</filter>

<filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

MyFilter.java:

public class MyFilter implements Filter {

    /** The skip equals ur is set. */
    private Set<String> skipEqualsURIsSet = new HashSet<String>();

    /** The skip starts with ur is set. */
    private Set<String> skipStartsWithURIsSet = new HashSet<String>();

    /** The skip ends with ur is set. */
    private Set<String> skipEndsWithURIsSet = new HashSet<String>();


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        String skipEqualsURIs = filterConfig.getInitParameter("skipEqualsURIs");
        if (skipEqualsURIs != null && skipEqualsURIs.trim().length() > 0) {
            for (String skipURI : skipEqualsURIs.split(",")) {
                skipEqualsURIsSet.add(skipURI);
            }
        }

        String skipStartsWithURIs = filterConfig.getInitParameter("skipStartsWithURIs");
        if (skipStartsWithURIs != null && skipStartsWithURIs.trim().length() > 0) {
            for (String skipURI : skipStartsWithURIs.split(",")) {
                skipStartsWithURIsSet.add(skipURI);
            }
        }

        String skipEndsWithURIs = filterConfig.getInitParameter("skipEndsWithURIs");
        if (skipEndsWithURIs != null && skipEndsWithURIs.trim().length() > 0) {
            for (String skipURI : skipEndsWithURIs.split(",")) {
                skipEndsWithURIsSet.add(skipURI);
            }
        }
    }


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String uri = httpServletRequest.getRequestURI();

        if (isSkip(uri)) {
            // do what you want to do based on skip logic
            // filterChain.doFilter(servletRequest, servletResponse);
        } else {
            // do what you want to do based on skip logic
            // filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    private boolean isSkip(String uri) {
        boolean skip = skipEqualsURIsSet.contains(uri);

        if (!skip) {
            for (String skipURI : skipStartsWithURIsSet) {
                if (uri.startsWith(skipURI)) {
                    skip = true;
                    break;
                }
            }
        }

        if (!skip) {
            for (String skipURI : skipEndsWithURIsSet) {
                if (uri.endsWith(skipURI)) {
                    skip = true;
                    break;
                }
            }
        }

        return skip;
    }

    @Override
    public void destroy() {

    }
}