Hello Sudharsan ,
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
<servlet-class>yogendra.MyServlet</servlet-class>
3) Executing the Servlet is as simple as running a normal file on LocalHost.
it would be something like :
http://localhost:8000/YOURAPPNAME/servlet/SERVLETCLASSNAME 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