Tomcat 5.0.28 problem - My servlet doesn't work

T

Thomas Hoheneder

Hello,

I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
have compiled a class TestingServlet.class with JDK 1.3.1 which is
recommended for use with Tomcat 5.0. Let me tell you, that this is just my
first servlet at all. For the work I use a book with the title "Java for the
Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
code is exactly the following, where the compiling went right:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class TestingServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}

As operating system I use Windows XP SP1 and my JAVA_HOME points to
"C:\jdk131" which I have also configured for Tomcat. Now, the book tells me
to put the TestingServlet.class file into a sub directory "myApps" of the
webapps directory, which is under my Tomcat installation path. Furthermore I
have a deployment descriptor named web.xml. So in a whole I have the
following files:
<TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
<TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
<TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class

My deployment descriptor (web.xml) looks like as follows:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>

Then I start the Tomcat server via the Tomcat monitor. At the URL
http://localhost:8080 I can see a standard starting page of Tomcat. Now the
book tells me that I can use the following URL to see my TestingServlet's
output:
http://localhost:8080/myApp/servlet/Testing

But this doesn't work. Instead of seeing the text "Welcome to the Servlet
Testing Center" I get the following error message in the browser window (I
use IE 6.0):


HTTP Status 404 - /myApp/servlet/Testing
------------------------------------------------------------------------
type Status report

message /myApp/servlet/Testing

description The requested resource (/myApp/servlet/Testing) is not
available.
----------------------------------------------------------------------------
Apache Tomcat/5.0.28


I have even tried the following URL's, but none of them lead to the result I
have expected:
http://localhost:8080/myApp/Testing
http://localhost:8080/servlet/Testing
http://localhost:8080/Testing
http://localhost:8080/myApp/servlet/TestingServlet
http://localhost:8080/myApp/TestingServlet
http://localhost:8080/servlet/TestingServlet
http://localhost:8080/TestingServlet
I always get the error above.

My question is: What went wrong? Why do I not get the text "Welcome to the
Servlet Testing Center" of my TestingServlet displayed in the browser
window?

Any help to this would be very appreciated. Thanks in advance.

Nice greetings from
Thomas
 
W

William Brogden

Hello,

I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
have compiled a class TestingServlet.class with JDK 1.3.1 which is
recommended for use with Tomcat 5.0. Let me tell you, that this is just
my
first servlet at all. For the work I use a book with the title "Java for
the
Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
code is exactly the following, where the compiling went right:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class TestingServlet extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}

As operating system I use Windows XP SP1 and my JAVA_HOME points to
"C:\jdk131" which I have also configured for Tomcat. Now, the book tells
me
to put the TestingServlet.class file into a sub directory "myApps" of the
webapps directory, which is under my Tomcat installation path.
Furthermore I
have a deployment descriptor named web.xml. So in a whole I have the
following files:
<TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
<TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
<TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class

Put all classes used in servlets into packages. Correctly name the
package in your web.xml and save the class files accordingly.

Old versions of servlet books and examples depended on the
"invoker" servlet which let you get away with classes in the
default package. BAD IDEA. See this FAQ at JavaRanch

http://faq.javaranch.com/view?InvokerServlet
 
J

John C. Bollinger

William said:
]
As operating system I use Windows XP SP1 and my JAVA_HOME points to
"C:\jdk131" which I have also configured for Tomcat. Now, the book
tells me
to put the TestingServlet.class file into a sub directory "myApps" of the
webapps directory, which is under my Tomcat installation path.
Furthermore I
have a deployment descriptor named web.xml. So in a whole I have the
following files:
<TOMCAT-HOME>/webapps/myApp/WEB-INF/TestingServlet.java
<TOMCAT-HOME>/webapps/myApp/WEB-INF/web.xml
<TOMCAT-HOME>/webapps/myApp/classes/TestingServlet.class


Put all classes used in servlets into packages. Correctly name the
package in your web.xml and save the class files accordingly.

Old versions of servlet books and examples depended on the
"invoker" servlet which let you get away with classes in the
default package. BAD IDEA. See this FAQ at JavaRanch

http://faq.javaranch.com/view?InvokerServlet

Also, the "classes" directory should go under WEB-INF, i.e.
<TOMCAT-HOME>/webapps/myApp/WEB-INF/classes/

When you have done as William advised and assigned your servlet class to
a package, its class file will need to appear in a corresponding
subdirectory of the classes directory, e.g.
classes/mypackage/TestingServlet.class if you assign the servlet to
package "mypackage".


John Bollinger
(e-mail address removed)
 
T

Thomas Hoheneder

Hello,

both William and John - your advices helped. It works now. Thank you very
much.

Nice greetings from
Thomas
 
W

Will Hartung

Thomas Hoheneder said:
My question is: What went wrong? Why do I not get the text "Welcome to the
Servlet Testing Center" of my TestingServlet displayed in the browser
window?

Couple of things, as others have mentioned.

One, you're relying on what they call the Invoker Servlet, which makes
Servlets in the web.xml magically appear. Tomcat disables this by default,
as it's a bit of a security hole.

Two, there's potentially the package issue. By not having your Servlet code
within a Java package, you are in store for a whole lot of grief. Save for
the most crude of example programs, and particularly within things like
Servlet containers, always use a package.

To get your example to work, do this:

add:
package mypackage;

to the top of your Java file.

Compile the file, and place the resulting class file in:
WEB-INF/classes/mypackage/TestingServlet.class

Then, fix your web.xml file, and replace:
<servlet-class>TestingServlet</servlet-class>

with:
<servlet-class>mypackage.TestingServlet</servlet-class>

Finally, again in the web.xml file, add:

<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/Testing</url-pattern>
</servlet-mapping>

Place that after your <servlet> tag.

That says "When you see the URL /Testing, call the Servlet name "Testing",
which is mapped to the 'mypackage.TestingServlet' class".

What the older invoker Servlet did was automatically create a
/servlet/ServletName servlet mapping for you. But, most web apps don't want
folks calling their Servlets directly. This was the default install behavior
in the past with Tomcat, but they removed it as many folks left this turned
on. You could still enable this behavior, but I don't suggest it.

After that, you should be good to go.

Good Luck!

Regards,

Will Hartung
([email protected])
 
J

Juha Laiho

Thomas Hoheneder said:
Hello,

I have a problem with getting a Java Servlet work within Tomcat 5.0.28. I
have compiled a class TestingServlet.class with JDK 1.3.1 which is
recommended for use with Tomcat 5.0. Let me tell you, that this is just my
first servlet at all. For the work I use a book with the title "Java for the
Web with Servlets, JSP and EJB" from Budi Kurniawan. My TestingServlet's
code is exactly the following, where the compiling went right:

In short, please read the 'First Webapp' chapter of Tomcat 5.0
documentation (included in Tomcat distribution, and also available at
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/index.html ).
It even includes a small demo application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top