0

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?

ASA
  • 1,911
  • 3
  • 20
  • 37
  • 1
    It looks like a mistake in your configuration file, because `/path1/*` should work like a charm – Andremoniy Apr 10 '14 at 13:30
  • read here about [URL patterns](http://www.roguewave.com/portals/0/products/hydraexpress/docs/3.5.0/html/rwsfservletug/4-3.html) – Braj Apr 10 '14 at 13:35
  • Andremoniy: Any idea where I could look? The web.xml of that application only has a few properly working and entries besides the welcome files. – ASA Apr 10 '14 at 13:46

1 Answers1

0

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() {

    }
}
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Is it coorect that it will work as if the filter wasn't there if I call filterChain.doFilter(request, response); ? I understand how to directly write to the request from the filter class/methods but how do return my servlet? Or should I put the servlet logic in the filter? – ASA Apr 10 '14 at 13:56
  • Filter works just like a servlet. use `filterChain.doFilter(request, response);` if you don't want to process this request means you want to skip this URL that is not relevant for this filter. – Braj Apr 10 '14 at 14:01
  • Yes you can put your servlet logic in this filter. I suggest you to put the logic in a separate file that is just like a service. – Braj Apr 10 '14 at 14:02
  • Every thing will go from this filter because its `url-pattern` is defined as `/*` means every request. You can check for cfg|xml|htm patterns in filter also. – Braj Apr 10 '14 at 14:05