This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Servlets and the fly likes new to servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "new to servlets" Watch "new to servlets" New topic
Author

new to servlets

sudharshan tettu
Ranch Hand

Joined: Jul 17, 2006
Posts: 124
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.
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
you have to create the web.xml file so that your servlet can be mapped to the name specified in the browser


i think therefore i am
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Originally posted by sudharsan tettu:
I am pretty new to servlets.


Simple Servlet Examples by Ben Souther are good for newbies. Just check them out!


My blood is tested +ve for Java.
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 212
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


Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
sudharshan tettu
Ranch Hand

Joined: Jul 17, 2006
Posts: 124
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???
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new to servlets
 
Similar Threads
standard CLASSPATH
Problem running servlets in tomcat 4.0.1
cache problem??? with Eclipse 3.2, Callisto, and Tomcat 5.5.17
my Web application servlets are not running
Starting out with Servlets and Tomcat 4