Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTP status 404 error from Tomcat

 
Rahul Divedi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm new to servlet to I'm testing my first servlet program but it is giving HTTP STATUS 404 error. The things which I have done are:
1> Installed java-> C:\Program Files\Java\jdk1.7.0
2> Installed Tomcat-> C:\apache-tomcat-7.0.23
3> When I type -> localhost:8080 in the browser it works fine.
4> Directory for the folder which I created-> C:\apache-tomcat-7.0.23\webapps\Hello
It contains a web.xml file and WEB-INF folder.
Inside WEB-INF, there is a folder named classes, which contains HelloServlet.class

The Souce code for my java file 'HelloServlet.java' is given below-

web.xml file code-


After reading few suggestions I tried by changing url to /HelloServlet.htm (or .html) but still I get the same error message.
******************************************************
HTTP Status 404 - /Hello/HelloServlet.html

type Status report

message /Hello/HelloServlet.html

description The requested resource (/Hello/HelloServlet.html) is not available.
Apache Tomcat/7.0.23
******************************************************
Log file catalina.2011-12-11 is-
Dec 11, 2011 8:25:59 AM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.22.
Dec 11, 2011 8:26:00 AM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [false], sendfile [true], accept filters [false], random [true].
Dec 11, 2011 8:26:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8080"]
Dec 11, 2011 8:26:07 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-apr-8009"]
Dec 11, 2011 8:26:07 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 9718 ms
Dec 11, 2011 8:26:07 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Dec 11, 2011 8:26:07 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.23
Dec 11, 2011 8:26:07 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\docs
Dec 11, 2011 8:26:11 AM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [1,313] milliseconds.
Dec 11, 2011 8:26:11 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\examples
Dec 11, 2011 8:26:12 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\Hello
Dec 11, 2011 8:26:13 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\host-manager
Dec 11, 2011 8:26:13 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\manager
Dec 11, 2011 8:26:13 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory C:\apache-tomcat-7.0.23\webapps\ROOT
Dec 11, 2011 8:26:13 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8080"]
Dec 11, 2011 8:26:13 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-apr-8009"]
Dec 11, 2011 8:26:14 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6679 ms

Please help me out as soon as possible. I'll be very much thankful to you.

Regards-
Rahul
 
Louis Bros
Ranch Hand
Posts: 54
Java Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think the problem is that you have mapped 'HelloServlet' to your servlet and not 'HelloServlet.html'.

Did you try accessing your servlet without the .html extension?

Louis
 
Rahul Divedi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply, yes I tried with .htm and with .htm (or .html) extension. I tried url-pattern as /hello/helloservlets and /HelloServlets also but nothing worked. Can there be any problem with the code or environment variables?

Regrards-
Rahul
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URL patterns are case sensitive. They are also exact, unless you use a wildcard. If you want to use /Hello/HelloServlet.html in the browser, the url-pattern value must be /Hello/HelloServlet.html as well. If you want also /Hello/HelloServlet, /Hello/HelloServlet.htm and even /Hello/HelloServlet.jsp or /Hello/HelloServlet.php, change the url-pattern value into /Hello/HelloServlet*.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the servlet container ignoring your servlet class because it's in the default package (no package)?

Try moving your servlet into a custom package (pick any unique name you want - "mypackage" should work for testing). I think the servlet container will use it then.

Also, if your web application context is "Hello", and your servlet's url-mapping is "/HelloServlet", the correct URI is "/Hello/HelloServlet" - case sensitive (since HTTP is case sensitive), and no .htm or .html suffix appended.
 
Louis Bros
Ranch Hand
Posts: 54
Java Tomcat Server Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your doGet parameters look as though they are the wrong way around.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Bros wrote:Your doGet parameters look as though they are the wrong way around.


This is where the @Override annotation can really help you.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, the deployment descriptor (web.xml) belongs inside WEB-INF.
 
Rahul Divedi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Pete Nelson]Is the servlet container ignoring your servlet class because it's in the default package (no package)?

Try moving your servlet into a custom package (pick any unique name you want - "mypackage" should work for testing). I think the servlet container will use it then.

Also, if your web application context is "Hello", and your servlet's url-mapping is "/HelloServlet", the correct URI is "/Hello/HelloServlet" - case sensitive (since HTTP is case sensitive), and no .htm or .html suffix appended.[/quote]

Hi Pete thanks for the reply. Can you please tell me should I put my HelloServlet.class file into 'mypackage' or entire class folder in this my package. Then what will be the url-pattern? Will it be /mypackage/HelloServlet?
For your second suggestion, should I add @Override annotation just above doGet like we do it in toString method?

Hi Bear,
I had read somewhere that web.xml file should be placed inside the folder which we create in webapps, so I placed it inside Hello but now after your suggestion I have moved it inside WEB-INF folder. Thanks.

Thanks Pete,Louis, Bear and Rob for such a prompt response.
 
Rahul Divedi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,
Can you please amend the necessary code with overriding which you told and re-post it. Is it necessary to use doPost also?
Thanks.
 
Rahul Divedi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Nelson wrote:
Louis Bros wrote:Your doGet parameters look as though they are the wrong way around.


This is where the @Override annotation can really help you.


Thanks a bunch to you guys and this forum, finally my code worked after deleting everything and doing it in a fresh way.
I'm really thankful for all the suggestions they really helped me. I moved my web.xml in side WEB-INF and added @Override to doGet method, after few errors it has worked finally.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic