Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Basic Hello World Servlet Program

 
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

I cannot get a simple hello world servlet program to work. Not sure where the issue is.

I am getting 404 when I am trying to access the url : http://localhost:8080/Hello_World/hello

The context root of the project is Hello_World.

There are no compilation issues.

Attached is the structure of the project and below is the code.




Structure-Hello-World-Servlet.png
[Thumbnail for Structure-Hello-World-Servlet.png]
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:


That's the package name, not the class name. You need to change it to com.javapapers.servlet.introduction.HelloServlet. That does mean you get 3 occurrences of HelloServlet in your web.xml, but that's because the name and the servlet class are not closely linked together. You could give the servlet any name you want, as long as it's unique in the application. The servlet-mapping refers to the name, so if you change it you must change it in the servlet-mapping as well.
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed it in web.xml and still I am getting 404.

Although if I use the annotation @WebServlet("/hello") on HelloWorld class, it works.

My question is why is it not working  if I used xml mapping in web.xml.

Below is my modified web.xml



I got the below logs  from tomcat . These are the logs when tomcat is starting. There is no logs updated when I access the url in the browser and getting 404. Not sure if the issue mentioned in the tomcat logs can cause 404.
Below are the tomcat logs:
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.56
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Dec 2 2021 14:30:07 UTC
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.56.0
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Mac OS X
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.16
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          x86_64
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           12.0.1+12
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         /Users/suhaasmohandos/Eclipse workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         /Users/suhaasmohandos/Downloads/apache-tomcat-9.0.56
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/suhaasmohandos/Eclipse workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/suhaasmohandos/Downloads/apache-tomcat-9.0.56
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/suhaasmohandos/Eclipse workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xbootclasspath/a:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/bootstrap.jar:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/commons-daemon.jar:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/tomcat-juli.jar
Jan 02, 2022 11:01:29 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [/Users/suhaasmohandos/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
Jan 02, 2022 11:01:30 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jan 02, 2022 11:01:30 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [1316] milliseconds
Jan 02, 2022 11:01:30 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Jan 02, 2022 11:01:30 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.56]
Jan 02, 2022 11:01:31 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jan 02, 2022 11:01:31 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [561] milliseconds
 
Master Rancher
Posts: 4606
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the HelloWorld.class file located?
 
Saloon Keeper
Posts: 24899
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "404" Page Not Found error occurs when a URL request cannot be resolved. This can happen within a web application when none of the webapp's mapping rules can resolve the URL. For example, when I ask for "foo.jsp" and I didn't create a "foo.jsp".

It can also happen if the webapp itself cannot be found, however.

Your Tomcat log doesn't say that it ever deployed any webapps at all. You're using WTP, however, and WTP does terrible things to Tomcat, so unlike standalone Tomcat or Tomcat run under the Eclipse Tomcat plugin, I can't be 100% sure whether anything really deployed or not.

A reminder that Tomcat, like every other JEE webapp container, is a web server and not a file server. So a properly deployed webapp must be in the form of a WAR and the WAR by default must have a name that matches the context-path part of your URL (Hello_World).
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the issue was resolved when I created the WebContent folder and web.xml while creating dynamic web project through eclipse instead of copying the WebContent from the sample project into my eclipse project. Surprising that we have to face such issues.
 
Tim Holloway
Saloon Keeper
Posts: 24899
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:Actually the issue was resolved when I created the WebContent folder and web.xml while creating dynamic web project through eclipse instead of copying the WebContent from the sample project into my eclipse project. Surprising that we have to face such issues.



No it's not, unfortunately. The problem with depending on elaborate IDE mechanisms is that the more elaborate the mechanism is, the more things you can do to confuse it.

As I said, the WTP plugin does bad things to Tomcat, which is why I use the Tomcat plugin instead. I don't actually set up "web projects" in Eclipse, I just set up regular projects and aim Tomcat at the project's Maven "target" directory. In other words, I'm doing almost everything manually. But I know what I'm doing, I'm not asking Eclipse to do the work for me, and thus I can't confuse Eclipse. Or myself.
 
Ranch Hand
Posts: 81
1
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:I changed it in web.xml and still I am getting 404.
I got the below logs  from tomcat . These are the logs when tomcat is starting. There is no logs updated when I access the url in the browser and getting 404. Not sure if the issue mentioned in the tomcat logs can cause 404.
Below are the tomcat logs:
...
INFO: CATALINA_HOME:         /Users/suhaasmohandos/Downloads/apache-tomcat-9.0.56
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/suhaasmohandos/Eclipse workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jan 02, 2022 11:01:29 PM org.apache.catalina.startup.VersionLoggerListener log
...
INFO: Command line argument: -Xbootclasspath/a:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/bootstrap.jar:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/commons-daemon.jar:/Users/suhaasmohandos/Downloads/apache-tomcat-10.0.14/bin/tomcat-juli.jar
Jan 02, 2022 11:01:29 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [/Users/suhaasmohandos/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]



Just my 2 cents and I am not sure whether this relates to the problem: the logs show mention both jar files from Tomcat 9 and Tomcat 10. This does not seam too healthy to me.
 
Tim Holloway
Saloon Keeper
Posts: 24899
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for information, no. "404" errors are not logged by either Tomcat or the webapp by default. They throw no exceptions and only appear as a standard server response to the client.
 
Roland Mueller
Ranch Hand
Posts: 81
1
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I changed it in web.xml and still I am getting 404.

Although if I use the annotation @WebServlet("/hello") on HelloWorld class, it works.

My question is why is it not working  if I used xml mapping in web.xml.




The web.xml is of version 2.4. Why annotations such like @WebServlet work? Annotation support  were only introduced in Servlet 3.0.

I think that the existence of the 2.4 web.xml should prevent that annotations work. If the servlet definition from web.xml does not work but annotations, my conclusion is that web.xml file from the project is not copied to the webapp in Tomcat.
 
Tim Holloway
Saloon Keeper
Posts: 24899
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just the XML schema. It's used by validating XML parsers when you edit the web.xml file. It's not used by Tomcat except maybe also to validate the XML.

If you use an antiquated XML schema, it may incorrectly flag newer features as errors when the web.xml itself is being digested, but it won't have any effect on annotations.

For that matter, if you use an antiquated schema and you don't use any newer web.xml elements, it won't matter either, since, as I said, the schema's sole purpose is to allow syntactic validation.

Of course, one would prefer to use the proper schema version. After all, you may just need one of those newer features someday. Or, for that matter, you may need to be alerted if you try to use an element that's no longer supported (although that really should be handled by Tomcat, not by removing from the schema - just to preserve backwards compatibility).
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic