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?
To add additional servlets, just add them. Each servlet extends the URL used by Vaadin.
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".
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>");
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:
Servers
> Tomcat
(or whatever) > /MyVaadin
, context+click to choose Undeploy.The new page should look something like this:
Now: 2014-03-17T13:58:19.916-07:00[America/Los_Angeles]
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.