Anthony,
On 1/10/23 13:58, Anthony Dell'Anno wrote:
I'm trying to run my first servlet on Tomcat
Welcome!
and am continually getting an HTTP Status 404 (I've also gotten 500
previously, with the root cause being an apparent compiler mismatch
(it would say that it's being compiled by version 63.0, which is Java
19, but that the latest version that was currently accepted was
version 59.0, or Java 15), but then after upgrading to JDK 19,
changing the JAVA_HOME variable and trying to run, it would still
give me the same error)?
Any error like "Unsupported class version 63.0" or similar will be due
to a JVM mismatch. There are three ways to correct this:
1. Compile with an earlier JVM
2. Run with a later JVM
3. Specify the target JVM with the compiler. This can be done with the
"-target" compiler switch in modern compilers
I have my Servlet, called HelloWorldServlet, located in the
"C:\apache-tomcat-10.1.4\webapps\ROOT\WEB-INF\classes\" directory
This isn't a good choice, but will work. I can get back to this, later.
Is your servlet compiled into a .class file, or do you have a .java file
in that directory (or both, which is fine)?
with the web.xml file being located outside of the classes folder,
directly inside of the WEB-INF folder.
That's where it belongs. The "ROOT" directory contains the entire web
application, and WEB-INF is a special directory which can contain:
classes/**/*.class [classes, usually your application]
lib/*.jar [libraries, usually from elsewhere]
web.xml [the deployment descriptor]
I've included both files. StackOverflow wasn't much help as of yet,
The mailing list often strips attachments, but your plain-text
attachment made it through. It's much less helpful as an attachment than
if it were inline, so I'm going to post it here so I can comment on it,
and also so others can read it without having to detach it:
(I've performed some light editing, which I hope you'll understand.)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Declare the XML version and encoding...open the document-->
<web-app>
<!-- Declare a web-app document-->
<servlet>
<!--Give the servlet's information-->
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>webapps.HelloXXXServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<!--Provide the servlet name-->
<url-pattern>/HelloWorld</url-pattern>
<!--Provide the servlet URL pattern-->
</servlet-mapping>
</web-app>
<!-- Close the web-app document-->
What is the name of the servlet class itself? You said you have a file
in WEB-INF/classes called HelloWorldServlet. If that's the class name,
then it should be in the file HelloWorldServlet.class. In your
configuration, you have servlet.HelloXXXServlet which is definitely wrong.
If your class name is actually HelloXXXServlet, then you need:
<!-- Declare a web-app document-->
<servlet>
<!--Give the servlet's information-->
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloXXXServlet</servlet-class>
</servlet>
The servlet class needs to be the "fully qualified name" of the class,
which will be the package name (which is nothing in your case) followed
by a period (if it's in a package, which it isn't) followed by the short
name of the class (HelloXXXServlet).
The "webapps." was completely incorrect, and it's not clear to me
exactly what the class name actually is, but I think you should be able
to get it from here.
The <servlet-name> is something you can make-up and is only used within
web.xml for the purposes of defining a <servlet> and then mapping it
later in <servlet-mapping> (and other things, actually, but you aren't
working with any of those quite yet).
Back to the "don't put your files into WEB-INF/classes" comment I made
above: it's a good idea to put all of your code into "packages". In
Java, that means:
1. Putting your class source e.g. HelloXXXServlet.java into a directory
which matches the package. So if your package will be
"anthony.dellanno", then you need to have your file in
src/anthony/dellanno/HelloXXXServlet.java. When compiled, this file
needs to go into WEB-INF/classes/anthony/dellanno/HelloXXXSServlet.class
2. In your .java file, you need to add at this at the top of the file:
package anthony.dellanno;
Once you do that, you'll change the "fully qualified class name" in
web.xml to this:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>anthony.dellanno.HelloXXXServlet</servlet-class>
</servlet>
And everything else is the same.
so I'm hoping that the Tomcat Users community can help me solve this
so that I can continue learning servlets. I'm working on building my
own software company.
I highly recommend that anyone working with Java Servlets actually read
the Java Servlet Specification -- whatever version makes sense for you
to read. Almost any of them would be good, since there is little change
between versions for the most part.
Nick Williams wrote a comprehensive (and I mean comprehensive!) book in
2014 which – despite its age (9 years ago) – is still entirely relevant.
It's called Professional Java for Web Applications. It guides you
through these basics and goes all the way up through databases,
WebSocket, and using the Spring Framework (which he favors in the book;
other frameworks are available as well).
You can also probably find something similar as your local library if
you don't want to pay $50 for a dead tree that takes up space on your
shelf forever.
Hope that helps,
-chris
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org