File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Could someone try and explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Could someone try and explain" Watch "Could someone try and explain" New topic
Author

Could someone try and explain

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
After days of torment of trying to run my HelloWorldServlet I managed to run my servlet via the backdoor.
I am using Tomcat so I naturally placed my servlet in the ROOT\WEB-INF\classes - which did not work.
I then placed the same HelloWorldServlet in the examples\WEB-INF\classes and accessed via a modified index.html page in the examples directory. It worked
I am unable to figure why this would be the case. It is obivous that the problem was with where the file was placed. Could anyone shed some light on this for me
thanks
Oh and thank you to everyone who replied to my last post.
david
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi David,

Welcome to Servlet development, and JavaRanch.

The best thing I can recommend is for you to download and read the servlet specification. Of particular interest in this case, is SRV.9.1 - SRV.9.5, plus SRV.11

This give the 'official' view of what a web application is, the directory structure of a web app, and how servlets 'map' to URLs

The other really great resource is the very large comments that are to be found inside Tomcat's default web.xml at CATALINA_HOME/conf/web.xml and Tomcat's documentation for server.xml, which is how you configure web applications in Tomcat

As a (very) high overview, you want to map your servlet classes to request URLs (in web.xml inside your webapp), and then register your webapp with Tomcat (in server.xml).

URLs:
TC config:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/index.html
webapp config:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/context.html
servlet spec:
http://jcp.org/aboutJava/communityprocess/first/jsr053/index.html
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

specifics:

Many tutorials (and some of them have really great other information, so it's hard to dismiss them) still use ROOT directory of Tomcat and use the 'lazy' mapping provided by 'most' (but not all) servlet containers... ie: "just place your servlet class here and call it like http://myserver.com/servlet/servlet.class.name"

While not 'wrong', this is not the best practice.

In fact, if you read web.xml I mentioned to you above, you'll see how Tomcat provides the /servlet/* pattern matching. It's in there in the comments.

What you'd want to do is create your own, web-application specific version of the web.xml file, which maps specific servlets to specific URLs. Then Tomcat can combine the webapplication name plus mapping provided in web.xml to serve the correct servlet.

eg: http://myserver.com/mywebapp/myservlet

if you mapped HelloWorld sevlet to 'myservlet' in the web.xml file in the 'mywebapp' application, then the above URL would invoke that servlet.
Martin Clausen
Greenhorn

Joined: May 15, 2001
Posts: 14
Hi David
Saw your posts regarding your Tomcat/Servlet troubles.
What version of Tomcat are you using ? this makes a diff., especially concerning the problems you describe.
I can recommend the www.coreservlets.com site. Has exellent info on basic Tomcat/Servlet/JSP configuration for major Tomcat versions.
And hey - let people know of you progress - preferably in this thread.
Martin
bernd pier
Ranch Hand

Joined: Jun 21, 2002
Posts: 30
hey david,
i want to give you a shoulder to lean on.
i`m just new in java and i know your feelings very well, but be sure there is a way to make servlet`s run.
don`t give up.

bernd
Mario Rendon
Greenhorn

Joined: Jul 11, 2002
Posts: 24
David
the response to your problem with your serlvet resides in the web.xml File .
first
you have to know a lttle of XML (Extended Markup Language ) to understand it; but is quite simple as html does ..

the web.xml is a place where you put and describe each servlet in your web application so...

look for the <Servlet Tag ..
and yo will realize that this tag gives a servlet a name; and also search for the ...
<servlet-mapping Tag and it maps . (relates) the servlet (defined previously) to the URL in the web application ..

so your URL will end like this..

http://localhost:8080/webappsname/mapping
Grettings
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Thank you for your help. I have got it working but I think everyone is right, I need to read up on the web.xml file. I am only new to this and I am working out of a book and that was the first stage - setting up and testing jsp and servlets.
I have uni exams in a couple of weeks so after that I will get back into it.

Thanks again for your help it is much appreciated.
David
 
Consider Paul's rocket mass heater.
 
subject: Could someone try and explain