Configuring Vaadin without web.xml

There is always room for improvement in programming. After my initial post about configuring Vaadin in simple 6 steps, my friend indicated that we could use annotation based configuration to get rid of web.xml in our Vaadin configuration. Yes, he is right. It is possible to configure Vaadin with annotations. Let’s see how it is;

Step 1: Add Servlet API dependency into your pom.xml, and update your project’s configuration


	javax.servlet
	javax.servlet-api
	3.1.0
	provided

Step 2: Modify your HelloWorldUI as follows

public class HelloWorldUI extends UI {
	
	@WebServlet(urlPatterns={"/*"},asyncSupported=true)
	@VaadinServletConfiguration(ui=HelloWorldUI.class,productionMode=false)
	public static class ExtendedVaadinServlet extends VaadinServlet {
	}

	protected void init(VaadinRequest request) {
		Button btn = new Button("Click Me!");
		btn.addClickListener(new ClickListener() {
			
			public void buttonclick(ClickEvent event) {
				Notification.show("Hello World!");
			}
		});
		setContent(btn);
	}
}

Step 3: Remove or comment out VaadinServlet configuration in web.xml

Just remove

 and 

elements in web.xml we had added in our previous post.


Now restart your web container, and try to access http://localhost:8080/helloworld URL to see the result. It should work as before.