Servlets Tomcat Execution in 7 Steps


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

http://tomcat.apache.org/download-60.cgi

Check with the documentation of what JDK or JRE version is compatible to the specific Tomcat version. For example I loaded Tomcat 5.0 and is compatible with 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: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: Creating your own 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: 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.

3. Now what is to be done?

Following next steps for Servlets Tomcat deployment and execution.

Step 4: Deployment of Servlet

Write a servlet program, say Validation.java 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: 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.

	
	  abcd
	  Validation
	

	
  	  abcd
	  /roses
	

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: Run the Tomcat server.

Step 7: 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.

For all programs, the same procedure of Servlets Tomcat execution is to be followed.

Note: Whenever the servlet file is copied (even the same one), it is required to restart the Tomcat every time.

Note: For the installation of Tomcat and running first program, it is advised to take the help of a friend knowing Servlets, else it takes a hell of time to execute the first program requiring lot of Google search.

12 thoughts on “Servlets Tomcat Execution in 7 Steps”

  1. devi siva priya

    Sir
    Is servelet api.jar can be download separately. But my tomcat does not not have this jar file.and my jdk is . 1.8.which tomcat version is suitable?and I have a doubt that while download ing tomcat from tomcat main website it show s zip.file and tar files.I don’t know which I have to download. My system is window s 7 32 bit.please which I have download.please clarify my doubt s sir
    I am struggleing for last month.
    Thank you sir

  2. sir,

    i have been trying since long time but i didn’t get executed above program.its not allowing to edit web.xml file and while executing it says javax package doen’t exist eventhough i appended servlet-api.jar file to my existing jdk path..please help me sir….and after installing there is no classes folder from versions 6 to 8.0..

  3. sir,

    In WEB-INF folder i didn’t get “classes” folder when i installed.Only web.xml file is present and not allowed to edit/modify it.

    Kindly suggest.

Leave a Comment

Your email address will not be published.