Pages

Wednesday, February 13, 2013

Serving Static HTML within WAR file

How to create a WAR file with static HTML pages inside

War file, as you know is a zipped folder. It must have the following structure

- sample-war.war
----- META-INF
-------- MANIFEST.MF
----- WEB-INF
-------- classes
-------- lib
-------- web.xml

Two important thing to remember:

  1. HTML file or folder that contains HTML files located inside WEB-INF will not be accessible to users
  2. HTML file or folder that contains HTML files located inside the WAR file, on the same level as WEB-INF will be visible to users

Let's consider the following structure, and see what will work and what will not
Assumption: your application deployed on localhost, port 8080 and the application name is sample-war

- sample-war.war
----- META-INF
-------- MANIFEST.MF
----- WEB-INF
-------- classes
-------- hidden-pages
----------- hidden-page.html
-------- lib
-------- web.xml
----- pages
-------- visible-page.html
----- index.html
----- outside.html

URI Is Page Visible
http://localhost:8080/sample-war/index.html true
http://localhost:8080/sample-war/outside.html true
http://localhost:8080/sample-war/pages/visible-page.html true
http://localhost:8080/sample-war/hidden-page.html false
http://localhost:8080/sample-war/hidden-pages/hidden-page.html false

Note: Normally you would like to enable this URI
http://localhost:8080/sample-war
and make it serve some home page for you, index.html for example.
In this case, simply add those lines to your web.xml

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

3 comments:

Christian said...

Do you have a github example?

Anonymous said...

Thanks, helped a lot!

Anonymous said...

Online casino site - LuckyClub.live
The online casino site provides games and services including slots, table games and roulette games with various features including real money bets. A  Rating: 7.8/10 · ‎11 votes · ‎Free · ‎Android · ‎Game 카지노사이트luckclub