Question

Besides my nice Vaadin 7.1 web app, I want to add a few plain servlets.

These other super-simple servlets provide simple pieces of text to their clients. Imagine a CurrentDateTime servlet that returns "2014-03-02T04:05:00Z" and a CurrentTemp servlet that returns "22C" servlet.

Why bundle more servlets with the Vaadin app?

  • Shared Resources
    These simple servlets share some resources (data, database connections, and such) in common with the main Vaadin app. So I would like to develop, package, and deploy them together, all in the same .war file.
  • Working Vaadin Project
    I cannot figure out how to create a simple Servlet-API-only Tomcat-compatible web app project in NetBeans 8 (beta) without all the Java EE crud. My Vaadin app is up and running with the help of the Vaadin Plugin For NetBeans, so I'd be glad to leverage that NetBeans Maven-based project.
Was it helpful?

Solution

Just Add A Servlet

To add additional servlets, just add them. Each servlet extends the URL used by Vaadin.

Step 1 – Build a Vaadin App

First use the Vaadin plugin 1.1.1 for NetBeans 8 to create a new Vaadin 7.1 app. Verify it runs properly.

Update: Vaadin 8 can be driven entirely by Maven. No need for an IDE plugin.

For example, say we have this Vaadin app running:
http://www.Example.com/MyVaadin/

Note that Vaadin also responds to these URLs in the same manner:

  • http://www.Example.com/MyVaadin/cat
  • http://www.Example.com/MyVaadin/dog
  • http://www.Example.com/MyVaadin/Now

The Vaadin app responds to those because of the wildcard asterisk in this annotation in the main Vaadin class:

@WebServlet( value = "/*", asyncSupported = true )

That slash and asterisk means "handle any URL extending the URL of our Vaadin web app". In this case "MyVaadin" is the base URL, so Vaadin responds to anything past that be it nothing, "cat", "dog", or "Now".

Step 2 – Add a Servlet

In the NetBeans Projects panel, context+click on Source Packages. Choose New > Servlet. Follow the wizard. Name the new servlet "Now".

Leave unchecked Add information to deployment descriptor (web.xml). You probably could turn that on, but nowadays in modern Java we can put configuration info in Annotations in the Java source code instead of the web.xml.

After the out.println( "<h1>Servlet Now at "… line, add this one:

out.println("<p>Now: " + java.time.ZonedDateTime.now() + "</p>");

That works for Java 8 and later. For earlier versions of Java, use:

out.println("<p>Now: " + new java.util.Date() + "</p>");

Step 3 - Run The New Servlet

Run your Vaadin app. Once started, add "Now" to the URL in the browser's address bar, and press Return. You should see a fresh page with the current date-time.

So this:
http://localhost:8080/MyVaadin/
becomes:
http://localhost:8080/MyVaadin/Now

If you do not see your new page, you need to freshen the Tomcat deployment. Try any of these actions:

  • Click the Build Project button in NetBeans (the hammer icon). (works for me)
  • Click the Clean And Build Project button in NetBeans (the hammer & broom icon).
  • On the Services panel in NetBeans, in the Servers > Tomcat (or whatever) > /MyVaadin, context+click to choose Undeploy.

The new page should look something like this:


Servlet Now at /MyVaadin

Now: 2014-03-17T13:58:19.916-07:00[America/Los_Angeles]


Explanation

Examine the annotation NetBeans placed on your Now servlet:
@WebServlet( name = "Now", urlPatterns = { "/Now" } )

That urlPatterns defines the extensions to the URL to which this servlet should respond. Apparently the Vaadin servlet’s pattern of /* defers to more specific patterns, in this case /Now. So /cat and /dog continue to be handled by the Vaadin servlet (given no other servlets mapped to those URLs), while /Now is handled by the "Now" servlet.

If someone can point out documentation for this behavior, somewhere in the Servlet spec I suppose, please post a comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top