Importing HTML and JavaScript

There are two ways to import HTML and JavaScript with a component.

By using the annotations @JavaScript and @HtmlImport

static class HtmlComponent extends Component implements HasText {
  // implementation omitted

Both annotations are repeatable and you can add multiple annotations for both to the component.

Another way of adding imports are the addHtmlImport(String url) and addJavaScript(String url) methods from the Page class. The functionality is the same as for the annotations.

  private void addDependencies() {

For every class that imports dependencies, import order is guaranteed for dependencies of the same type only. No guarantees are given regarding numerous classes' dependencies' import order, same as no guarantees are given on import order of dependencies of not the same type.


static class OrderedDependencies extends Div {

In this case, 1.js will be imported before 2.js, 1.css before 2.css, 1.html before 2.html, but no other guarantees can be made.

If you want to enforce an order between dependencies of a different type, you can add html import with imports in it. For example, if htmlimport4.html should be run before htmlimport4-js.js then you should import an html that has the correct load order:

<link rel="import" href="htmlimport4.html">
<script src="htmlimport4-js.js"></script>

You can place your static resources in any folder inside your WAR file except for /VAADIN which is reserved for framework internal use. VaadinServlet handles static resource requests if you have mapped it to /* . Otherwise, the servlet container will take care of static resource requests.

By using relative URLs you are not dependent on whether the application is deployed in the root context (e.g. or in a sub context (e.g.

Relative URLs are resolved using the page base URI, which is always set to match the servlet URL.


If you are using a servlet path for the servlet, e.g. then you will need to take the servlet path into account when including resources. This is needed because the base URI is but static resources are deployed in

You can use the special protocol context:// with e.g. Page.addHtmlImport to ensure a URL relative to the context path but this is only supported when including resources.

When you configure an element, e.g setting the src attribute for an <img>, you cannot use the context:// protocol. Your options are then:

  • Cancel out the servlet path, e.g. ../images/logo.png.

  • Use an absolute URL, e.g. /myapp/images/logo.png

  • Deploy your static resources in a directory matching the servlet path, e.g. /myservlet/.


There is a possibility to define which dependencies are loaded first, refer to Ways of importing the dependencies for details.