O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

WAR files

A WAR file is simply a snapshot of your web app structure, in a nice portable, compressed form (it’s really just a JAR file). You jar up your entire web app structure (minus the web app context directory—the one that’s above WEB-INF), and give it a .war extension. But that does leave one problem—if you don’t include the specific web app directory (BeerApp, for example), how does the Container know the name/ context of this web app?

That depends on your Container. In Tomcat, the name of the WAR file becomes the web app name! Imagine you deploy BeerApp as a normal directory structure under tomcat/webapps/BeerApp. To deploy it as a WAR file, you jar up everything in the BeerApp directory (but not the BeerApp directory itself), then name the resulting JAR file BeerApp.war. Then you drop the BeerApp.war file into the tomcat/webapps directory. That’s it. Tomcat unpacks the WAR file, and creates the web app context directory using the name of the WAR file. But again, your Container may handle WAR deployment and naming differently. What matters to us here is what’s required by the spec, and the answer is—it makes almost no difference whether the app is deployed in or out of a WAR! In other words, you still need WEB-INF, web.xml, etc. Everything on the previous pages applies.

Almost everything. There is one thing you can do when you use a WAR file that you can’t do when you deploy without one—declare library dependencies.

In a WAR file, you can declare library dependencies in the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required