Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin steals all Url Patterns

tom howe
1 decade ago Mar 22, 2011 11:31am
Marko Grönroos
1 decade ago Mar 22, 2011 4:51pm
tom howe
1 decade ago Mar 24, 2011 8:48am
Bobby Bissett
1 decade ago Apr 14, 2011 4:33pm

Howdy,

In case it's not obvious how this works, a url pattern like "/info/a" does not match "/info" but it does match "/*" since the wildcard would match with anything (in this case * == "info/a"). Given two patterns like "/*" and "/info/*", the rule is that the longest match wins, so the second one wins.

I'm currently using Vaadin as the UI front end on a service that provides REST URLs for various things. In my case I wanted to put the web app at /ui and have everything else be for the REST services. But, if a user comes to the main page, I want him/her to be shown the UI. This worked fine for me, and I'm sharing it in case anyone is curious about how the mapping works. A similar pattern, for instance, could be used to map /* to Vaadin but /login.jsp to a JSP if you're using form-based authentication.

In my case, I'm using Java EE 6 so I have the Vaadin mappings in a servlet class like this. The mappings could just as easily be in web.xml:

@WebServlet(urlPatterns = {"/ui/*", "/VAADIN/*"})
    public static class MyServlet extends AbstractApplicationServlet {
        // ...
    }

Then web.xml contains:

<servlet>
        <!-- technically, a JSP is a servlet -->
        <servlet-name>index</servlet-name>
        <jsp-file>index.jsp</jsp-file>
    </servlet>
    <servlet>
        <!-- this is the Jersey REST servlet -->
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- We'll map / to the welcome page and /* to our resources. -->
    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

So I have it set up that /* will match to any REST resource I add, as long as it's not /ui/*. When a user first comes to the site, s/he is directed to index.jsp, which is simply:

<%
    response.sendRedirect("ui");
%>

That's probably a lot more than you want to know, but maybe it will help someone. If the UI is a minor part of the web application, then I like having it sit at a specific URL rather than using /* for it.

Cheers,
Bobby