• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

http status 404 servlet

 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to access URL : localhost:8080/ch1/Serv1

I get error status - HTTP Status 404 - /ch1/Serv1.htm requested error resource is not available
instead of the page displaying current date.


web.xml


ch1servlet.java


Directory structure is :
C:\apache-tomcat-7.0.34\webapps\ch1\WEB-INF\classes\ch1servlet.class
C:\apache-tomcat-7.0.34\webapps\ch1\WEB-INF\web.xml



The tomcat server is running without trouble.
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a simple deployment issue. I don't see anything wrong with your web.xml or Servlet class (though it should be in a package of some type).

Take a look at Marty Hall's Servlet Tutorial. It may help you find a step you're missing.

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-First-Servlets.html

-Mike
 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to figure it out. What step could I have missed?
I've created the directory structure that I've mentioned and copied the .class(of the .java file given) and web.xml file.
 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by deployment issue exactly?
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A deployment issue can occur if all the files are not exactly where they're expected.

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!

Good luck.

- Mike
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.

Bill
 
Richard G Hayward
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the exact same problem as sidharth.
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.

Regards
Richard
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard G Hayward wrote:However K&B explicitly say (p30) that to keep the example simple this servlet isn't in a package.

No longer true. Put the class in a package.
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill


Hi Bill,

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.

Thanks,

- Mike
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes an unpackaged servlet can work. Sometimes. It's best not to rely upon the convergence of celestial bodies and to always package all classes.
 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was something wrong with the web.xml header, I guess.
As Richard said, it is the first example in the Head First Servlets and JSPs book.
I replaced it with the header from another HelloWorld servlet, and it started working.



Updated Web.xml:



 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, for me it works both with and without packages.
But point noted Bear
Bear Bibeault wrote:Sometimes an unpackaged servlet can work. Sometimes. It's best not to rely upon the convergence of celestial bodies and to always package all classes.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of like saying "I run stop signs all the time and I've never been hit".
 
sidharth mittal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean for this instance. This is my first Servlet web app.
But, I'll keep that in mind here on.
 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've found that this is an error in the book.

The first line of web.xml should be

<?xml version="1.0" encoding="ISO-8859-1"?>

instead of

<?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:

http://www.coderanch.com/t/418435/java-Web-Component-SCWCD/certification/HTTP-Status-ch-Serv

The error is also listed on O'Reilly's errata page:

http://oreilly.com/catalog/errata.csp?isbn=9780596005405

Regards
Richard
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very early in this history of Servlets, the designers had this great idea to simplify early experimentation.

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.

Bill


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic