When we install Tomcat Server for the first time then we have to configure it for its proper functioning. You must have written a servlet code and compiled it(may be). Now you want to run it, and see some wonder in your browser page and pat your back. Isn’t it? Ok.
Now you must have installed your tomcat with its default setting. In my case it is installed in
C:\Program Files\Apache Software Foundation\Tomcat 6.0….
Ok. Now you have to set the path for javax.servlet.*; and javax.servlet.http.*; package. Isn’t it? Because when ever you will be compiling you Java code. Your Java compiler will be giving some error that javax.servlet package not available.
So you have to search a directory named lib inside your installed Tomcat folder. In my case it’s
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib
Now in that lib folder there will be a jar file named servlet-api.jar. You have to set the classpath for that.Open command prompt (holding windows key, press R and type CMD). Then type
classpath=C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;
(make appropriate changes according to your tomcat directory and lib directory path).
But this not permanent. You have to set this path every time you start your computer. If you want to make it permanent, you have to set environment variable. The steps are as follows…
RightClick on my computer->Properties->Advance->Environment Variables--
->System Variables->New
Variable name=Classpath
Variable Value= C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;.;
(make appropriate changes according to your Tomcat directory and lib directory path).
Now you are done with the class path. Now go to the bin directory of your Tomcat Server. In my case it’s
C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin
And double click on Tomcat.exe(service runner). Your tomcat will start and a command prompt will be visible in which some characters and strings will be flowing. The last string will be some milliseconds.
Now just recompile your .java code. If everything went well. You will successfully compile it and a .class file will be generated. Now you have to follow some directory hierarchy to place this .class file. The hierarchy must be like this.
You should search for a directory named webapps (in my case C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps).
In that folder you have to create a subfolder of any name(like xyz). Inside xyz there must be a directory named in the same casing as WEB-INF, inside WEB-INF create a file(names web.xml) and a folder(named classes), inside classes you will place your .class file. You will write class name and url-pattern in the web.xml file.
In some cases some other program have occupied port 8080, and for this reason also your tomcat will not work properly, you will get this message in the command prompt which came in vision when you double clicked Tomcat.exe service runner. Here is the process to change the port number. Search for the conf folder and edit server.xml(change the port number “Connector port=8080”). You can change/see your admin password in the same folder in a file named tomcat-users.xml.
Now you are done, say all is well and open a browser and type
http://127.0.0.1/xyz/YourServletUrl or http://localhost/xyz/YourServletUrl
where xyz in the folder and YourServletUrl is the written in web.xml
I think if you will go through all these steps according to your requirement, your tomcat will run properly and your servlet will give the expected output.