Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes Beginner's Servlet Not Found Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Beginner Watch "Beginner New topic
Author

Beginner's Servlet Not Found Error

Gale Greaser
Greenhorn

Joined: Aug 05, 2002
Posts: 17
Hello -
I am attempting to deploy my very first servlet on Tomcat following the directions in "More Servlets and Java Server Pages" by Marty Hall.
This is my Error:
4.0.4 -- "The requested resource(/webapp1/servlet/testservlet.HelloWebApp) is not available."

This is my directory structure:
webapps
---webapp1 - HelloWebApp.jsp
------WEB-INF - web.xml
---------classes
------------testservlet - HelloWebApp.class
I am using the URL:
http://localhost/webapp1/servlet/testservlet.HelloWebApp
And I edited the server.xml in the conf directory to include:
- <!-- Tomcat Root Context -->
<Context path="/webapp1" docBase="webapp1" debug="0" />
The HelloWebApp.jsp works just fine - no problems.
Its just the servlet that I'm having difficulties with. I have checked everything I know to check many times... Is there something I am missing?
Perhaps something in the server.xml file?
Much thanks!
Gale
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I would not recommend this method of deploying servlets.

Search these forums for 'servlet-mapping' or read up on web.xml file. This is a much better way to do things.

Using the /servlet/* mapping, plus the actual class name of the servlet is a 'lazy' way of deploying servlets.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Let's create a context step by step:
#1 create your context root directory
Let's suppose the name of your Tomcat install directory is catalina.
In C:\catalina\webapps create a new folder, say myContext. Inside myContext, create a WEB-INF folder (all caps). Inside WEB-INF, create a classes and lib folders.
Go to C:\catalina\conf and copy the web.xml file there to C:\catalina\webapps\myContext\WEB-INF. Edit this web.xml so that you will have

Go to C:\catalina\conf and edit the server.xml file. Look for this entry:

Under it, insert this:

Putting <!-- User Defined Contexts --> is strictly unnecessary, of course, but I find it helpful to find my contexts.
Now all we need to do is test the setup. Create a simple JSP called index.jsp:

and put it in C:\catalina\webapps\myContext.
Go to C:\catalina\bin and type startup to start Tomcat. A DOS window will appear with the following message:

indicating Tomcat started normally.
Open a browser and type in this URL: http://localhost:8080/myContext
If your index page shows up, you have defined your context successfully.
#2 Create a servlet and deploy it
Code a simple servlet for testing purposes:

Put TemplateServlet.java in C:\catalina\webapps\myContext\WEB-INF\classes.
Get your favorite IDE, compile it, so that the resulting TemplateServlet.class is in the same folder.
(At this point some will argue that there is no need to put source code inside the classes folder, that you can use the javac -d option, etc. These are all valid points, but I'm trying to keep it simple now.)
Using the browser, type in this URL: http://localhost:8080/myContext/servlet/TemplateServlet
and you should get Test.
Note that we have NOT edited the web.xml file.
#3 registering the servlet in the web.xml file
Go to C:\catalina\webapps\myContext\WEB-INF and edit the web.xml so that we have:

Use only Notepad or DOS edit. Do NOT use WordPad or other editors as it will change the formatting.
We have now "registered" the TemplateServlet with Tomcat with the alias template and it has the servlet path /test.
Please note that if you edit any Tomcat config file like web.xml or server.xml you have to restart Tomcat. After shutting Tomcat down, do not start up again immediately, since it is still releasing resources. The most common error of this type is a bind exception on the 8080 HTTP port that Tomcat uses.
After restarting Tomcat, type in this URL now:
http://localhost:8080/myContext/test
and you should get the same servlet. Alternatively, you could use its alias, e.g
http://localhost:8080/myContext/servlet/template
Brusk Baran
Ranch Hand

Joined: Nov 15, 2001
Posts: 132
<!-- Tomcat Root Context -->
<!--
<Context path="" docBase="ROOT" debug="0"/>
-->
Should we get rid of the comments here, in order to run ROOT, or does ROOT HET INVOLVED SOMEHOW DIFFERENTLY??
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginner's Servlet Not Found Error
 
Similar Threads
HTTP Status 404 -- Apache Tomcat/4.1.18
Error 500- Internal servlet error
Root Context - mywebapps has no effect
setting default context in Tomcat
Resource not allowed