Hi I am pretty new to servlets.I installed tomcat and compiled successfully a servlet.I placed the class file insdie web-inf/classes I dont know what all the configuration I should do before ,runnin a servlet successfully on TOMCAT. Please guide me in getting familar with workin servlets on Tomcat Thanks Sudarsan.
Any Servlet has 3 parts of getting it working with Tomcat or any Application Server you are using. I am not sure about other application servers, However the question in place is of Tomcat so i would be best able to help you.
The 3 parts which i am talking about are
1) Compiling - Which you have done till now. 2) Deployment - What Nelson is Talking about 3) Execution - which is explained below in part 3.
The first part is already completed by you. Now , As Nelson said , You need to create a web.xml file which is more formally called as an Deployment Descriptor. Its called so because Every Deployment information about your servlets or any other Context Related information is entered here. So, Deployment is very simple
For the servlet which you have Compiled , Add an entry in web.xml file, Make sure when you name this file, It is web.xml and not web.xml.txt. This file will be situated in your Application's WEB-INF directory.
Entry should be something like this :
<web-app> <servlet> <servlet-name>ANY NAME YOU WISH</servlet-name> <servlet-class>YOUR SERVLET CLASSNAME</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SHOULD BE THE SAME NAME DEFINED IN ABOVE <servlet-name element> <url-pattern>/servlet/YOURSERVLETNAME</url-pattern> </servlet-mapping> </web-app>
<load-on-startup> element is the element of web.xml which loads the servlet instance in the memory so that the first request to the client also is fast. For more information on web.xml , follow this WEB.XML Description
Also if your servlet is defined in a package then the <servlet-class> element should contain the packagename.YourClassName , So it would be something like
3) Executing the Servlet is as simple as running a normal file on LocalHost. it would be something like :
Hope this helps you , Any further help , please get in touch with us again. We here at Ranch love helping our fellow Ranchers !
Best Regards , Yogendra Joshi
Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
Joined: Jul 17, 2006
ya its working ..thanks for your replies... and one more doubt is like ..does it work ,just copy and pasting a war file inside web-apps and just calling it...else should we configure it anywhere in deployment descriptors???
There is a deployment descriptor inside each war file. Once you drop it in the webapps directory, Tomcat will uncpack it and deploy it. It will create a folder with the same name as the war file. Look in the WEB-INF directory of the application for the deployment descriptor (web.xml)