2 Minutes To Code (β Version)

Java Servlets - 2 minute tutorial

How to create a war file

Now that we have written our first servlet and compiled the servlet, it's time to create a war file with the servlet in it.

To create the war file, we will use the jar command.

A war file has a given structure, it has a WEB_INF folder with classes and lin sub-folders for compiled packages and jar files respectively. Besides, the WEB-INF folder also contains the deployment descriptor web.xml. Any public resourec like jsp, images, js etc lie outside the WEB-INF folder as shown in the image below.

Typical structure of the contents in the .war file :



A sample deploument descriptor web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app> 
	
	<servlet>  
      <servlet-name>welcomePage</servlet-name>  
      <servlet-class>com.techfundaes.servletsBag.FirstServlet</servlet-class>  
   </servlet>  
  
   <servlet-mapping>  
      <servlet-name>welcomePage</servlet-name> 
      <url-pattern>/firstPage.do</url-pattern>  
   </servlet-mapping> 

</web-app>
Now, let's see the comamnd to create the war file myApp.war :

jar -cf myApp.war *

will put all contents of the current folder in the war file myApp.war as shown in the image below.