5

I created a simple web application that consists of a dir with html, css, js. No server code. For reasons complicated to explain, my administrator insists on turning it into a .war file, so that it can easily be deployed on tomcat. Again, the application does not contain any Java code at all. I didn't create it with ant or eclipse.

I tried creating an archive:

jar cvf mywarfile.war *

Which does indeed create a war file. However, when I deploy this war on tomcat, it doesn't seem to work. Tomcat automatically extracts the war file to a directory but it does not become available through the web server. I guess it has something to do with that my war does not contain directories WEB-INF and META-INF.

Is there an easy way I can turn a static client side web application into a deployable war file?

Jeroen Ooms
  • 2,187
  • 7
  • 32
  • 51

2 Answers2

4

In order to deploy content on tomcat, one has to include a file /WEB-INF/web.xml. Below an example of a web.xml that I used to deploy some static content through tomcat.

<?xml version="1.0" encoding="UTF-8"?>

<web-app>
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.json</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.csv</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
         <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpeg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>csv</extension>
        <mime-type>text/csv</mime-type>
    </mime-mapping>      

    <mime-mapping>
        <extension>css</extension>
        <mime-type>text/css</mime-type>
    </mime-mapping>  

    <mime-mapping>
        <extension>js</extension>
        <mime-type>application/javascript</mime-type>
    </mime-mapping>     

    <mime-mapping>
        <extension>html</extension>
        <mime-type>text/html</mime-type>
    </mime-mapping>     

  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Restricted URLs</web-resource-name>
      <url-pattern>/</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
  </security-constraint>

  <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

</web-app>
Jeroen Ooms
  • 2,187
  • 7
  • 32
  • 51
4

With Tomcat 7 and newer you don't even need a web.xml

Just zip your index.html file or wharever resources you need. Rename the mini.zip to mini.war and thats it.

mini.war
--index.html
R.A
  • 141
  • 3