Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Tomcat 5.0.28 problem - My servlet doesn't work

Reply
Thread Tools

Tomcat 5.0.28 problem - My servlet doesn't work

 
 
Thomas Hoheneder
Guest
Posts: n/a
 
      10-12-2004
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"
xmlnssi="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





----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
 
William Brogden
Guest
Posts: n/a
 
      10-12-2004
On Tue, 12 Oct 2004 18:28:30 +0200, Thomas Hoheneder
<(E-Mail Removed)> wrote:

> 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


>
> 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"
> xmlnssi="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

 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      10-12-2004
William Brogden wrote:

> On Tue, 12 Oct 2004 18:28:30 +0200, Thomas Hoheneder
> <(E-Mail Removed)> wrote:


[...]

>> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Thomas Hoheneder
Guest
Posts: n/a
 
      10-12-2004
Hello,

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

Nice greetings from
Thomas




----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
 
Reply With Quote
 
Will Hartung
Guest
Posts: n/a
 
      10-12-2004
"Thomas Hoheneder" <(E-Mail Removed)> wrote in message
news:416c0429$1_4@127.0.0.1...
> 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
((E-Mail Removed))



 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      10-14-2004
"Thomas Hoheneder" <(E-Mail Removed)> 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/tom...doc/index.html ).
It even includes a small demo application.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tomcat ssl servlet to servlet, private CA edavid3001@gmail.com Java 0 10-06-2005 09:03 PM
problem running a basic servlet with tomcat Ryan Gaffuri Java 7 09-27-2004 06:42 PM
seeking servlet "Master" keep getting "Master/servlet/Master" not found. Tomcat 5.0.25 Doug McCann Java 1 08-05-2004 09:16 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
Tomcat: POST from one servlet or internal class to another servlet Sean Clarke Java 1 01-07-2004 02:22 PM



Advertisments