I get error status - HTTP Status 404 - /ch1/Serv1.htm requested error resource is not available
instead of the page displaying current date.
Directory structure is :
The tomcat server is running without trouble.
Take a look at Marty Hall's Servlet Tutorial. It may help you find a step you're missing.
From your reply it sounds like you have them correct, yet something's still not working, right? Again, not unusual at all. Happens to everybody initially and, gulp, even later...
I'm assuming you can type a http://localhost:8080 and see the Tomcat welcome screen.
(I've seen cases where students have taken a day or two to figure out how to get a basic Servlet working.)
There are many different approaches here.
What I do in these cases is to use an IDE that will create a Servlet and deploy it for you so the problem doesn't happen in the first place. You could still do that and then examine the structure/code/etc to what you did to see what went wrong and adjust accordingly the next time.
Or, if you don't want to try the IDE route, it's a still good idea to walk through a tutorial, like the one I sent you a link to and verify it works as expected. I always create a simple test project outside the one I'm having an issue with to verify and do a sanity check. Your Servlet is simple to begin with, but a tutorial walk-through on a separate example is still a good idea.
There's always eventually an "Ah hah!" moment where it all makes sense.
I'd recommend picking up a copy of Marty Hall's Core Servlets and JavaServer pages. Marty rocks!
It's the very first example in K&B's Head First Servlets & JSP.
William says it's a common beginner mistake to not put a class in a package.
However K&B explicitly say (p30) that to keep the example simple this servlet isn't in a package.
In my case, I've compiled Ch1Servlet.java on my laptop & am trying to deploy it to a remote Linux host running Tomcat 7.0.26.
As my laptop doesn't have Tomcat installed, I downloaded servlet-api.jar from the server and used the javac -classpath switch to put it somewhere where the compiler could find it. Does it matter where the compile time location for servlet-api.jar is?
Strangely, I also have a local Windows server running Tomcat 7.0.21. On that server, the same example, with files in the same locations, works ok.
William Brogden wrote:
A common beginner mistake.
ALL classes used in servlets MUST be in packages and the class file in the corresponding directory. The reason being that without a specified package, the JVM looks in the "current" directory - something you have no control over.
My test Servlet works fine for me whether it's in a package or just in the src directory -- deployed it both ways, but I would always use a package as you recommended and per my initial posting above.
But, I'll keep that in mind here on.
The first line of web.xml should be
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="ISO-8851-1"?>
Since posting earlier, I've discovered that this same issue has been raised in this forum several time before, for example:
The error is also listed on O'Reilly's errata page:
The DREADED INVOKER SERVLET.
The invoker let you have a servlet without a package by running it indirectly, /servlet/X links would go through the invoker which would then find and load the named servlet X.
The confusion and misery this decision caused is astronomical.
All modern distributions of Tomcat have had the invoker commented out in web.xml but it is still in the library.