Servlet WAR File Tomcat Creation Deployment Execution

Servlet WAR File Tomcat Creation Deployment Execution

Servlet WAR File Tomcat is explained step-by-step and follow it.

We have seen earlier what a JAR file is, its options, creation and advantages in JAR (Java ARchive) Files.

Now let us study what a WAR file is and its creation etc. WAR stands for Web ARchive. A WAR file is a JAR file, but contains Web components only. What does it mean? A JAR can contains any files compressed like text files, doc files, gif files and excel sheets etc. A WAR file is also a compressed file like JAR but contains only the Web components like Servlets, JSPs, web.xml, JavaBeans, Struts and HTML pages etc.

The command to create a WAR file is JAR only (executed from command-prompt) but extension is .war.

Follow the same 7 Steps – Servlets Tomcat Execution, anyhow a few steps required are added to the existing and reproduced here.

Install Tomcat server of your choice version by downloading from Apache site. Many versions of Tomcat can be found in the following link.

Check with the documentation of what JDK or JRE version is compatible to the specific Tomcat version. For example I have loaded Tomcat 5.0 and got compatible JDK 6.0.

While installation, it asks the port number and I have entered 8888 (default is displayed as 8080. I have not preferred 8080 for the reason on many systems Oracle server will be working on 8080. For this reason better choose a different port number). Later, give your own password.

Step 1 of Servlet WAR File Tomcat: Place the default servlet-api.jar file in classpath

When installed, Tomcat gives many folders of which a few are given hereunder used for execution. See the following one.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;

Keep the above JAR file in the classpath of Windows Environment Variables, else the servlet program will not be compiled.

Note: Now, a fresher should be careful here in the following steps. Steps are very simple but should be followed carefully. Any small mistake committed, Tomcat simply refuses to execute your Servlet.

Step 2 of Servlet WAR File Tomcat: Creating your own (base)directory structure.

You also get the following folders.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps

In the above webapps folder create your own new folder. I created and named it as "india".

Step 3 of Servlet WAR File Tomcat: Observe the following directory structure.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\ROOT\WEB-INF

Just copy the above WEB-INF folder (ofcourse, along with its subdirectories) into india folder.

When you did, now you get the following structure. Check it.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\india\WEB-INF

Note 1: Do not confuse now with the WEB-INF folder as WEB-INF exists in two places – one in ROOT and one in india.

Note 2: Tomcat is case-sensitive. Do not write web-inf instead of WEB-INF etc.

Remember, now onwards when I talk about WEB-INF folder, I mean the WEB-INF available in india and not in ROOT. This is very important.

In \webapps\india\WEB-INF folder you get automatically one "classes" folder and one "web.xml" file.

Now what is to be done?

Step 4 of Servlet WAR File Tomcat: Deployment of Servlet

Write a servlet program, say in your current directoty and compile it as usual with javac command and obtain Validation.class. Copy the Validation.class file from your current directory to classes folder available in the following classpath.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\india\WEB-INF\clssses

Copying the .class file of servlet to classes folder is known as deployment.

Step 5 of Servlet WAR File Tomcat: Giving the alias name to the servlet with Deployment Descriptor.

Open the web.xml file available in the following folder.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\india\WEB-INF

In the web.xml file add the following code just before </web-app> tag.

Now the alias name of Validation servlet is roses. Client should call the Validation servlet with the name roses. Observe, this roses you used in HTML file.

The web.xml file is known as deployment descriptor as it describes the deployment information of a servlet. Learn more at Deployment Descriptor – web.xml in Servlets.

Step 6 of Servlet WAR File Tomcat: Creating WAR File

From here starts creating WAR file. Now you are ready with india folder.

Come to DOS prompt as follows where india folder exists.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\india>jar -cvf hyderabad.war .

Note: Do not forget to place dot after hyderabad.war which indicates to include the files of india and also subdirectories of india.

Now in india folder, a WAR file by name hyderabad.war is created. This hyderabad.war file contains all the Web components to execute a Servlet.

Step 7 of Servlet WAR File Tomcat: Deployment of WAR file

Copy this hyderabad.war file to webapps folder. Remember, webapps is available in the following path.

C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps

Now creation and deployment of WAR file is over. Next step is execution. Include the WAR file name in the client request as follows.

Earlier remember, in place of hyderabad it is written india.

Following is the modified client HTML file:

Observe, hyderabad in the action is the WAR file name.

The same Validate servlet of Login Screen Validation is used without any modification.

Step 8 of Servlet WAR File Tomcat: Execution, Run the Tomcat server.
Step 9 of Servlet WAR File Tomcat: Open a browser.

Open the UserPass.html file from File Menu of the browser, enter the user name and password and click Send button. Now you get VALID or INVALID.

A similar program is available but creating WAR file in Weblogic server WAR File in Weblogic Creation and Deployment.

Leave a Reply

Your email address will not be published. Required fields are marked *