Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simplest Possible Servlet

2 views
Skip to first unread message

R@yZor

unread,
May 15, 2001, 1:42:26 AM5/15/01
to
Here is the simplest possible servlet from the Core Servlets book from Sun
by Marty Hall:

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

public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Hello World");
}
}

This code (above) runs perfectly on my JRun server. When I try it on my
Tomcat I get an error 500 (see below):

Internal Servlet Error:

java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)

-----rest of error here.......

You might be thinking I don't have the class file on the correct directory
but another hello world example from Tomcat is working perfectly on the same
directory. Here is the code for the one that works.

/* $Id: HelloWorldExample.java,v 1.2.4.1 2000/07/05 17:45:01 nacho Exp $
*
*/

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

/**
* The simplest possible servlet.
*
* @author James Duncan Davidson
*/

public class HelloWorldExample extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");

String title = rb.getString("helloworld.title");

out.println("<title>" + title + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<body>");

// note that all links are created to be relative. this
// ensures that we can move the web application that this
// servlet belongs to to a different place in the url
// tree and not have any harmful side effects.

// XXX
// making these absolute till we work out the
// addition of a PathInfo issue

out.println("<a href=\"/examples/servlets/helloworld.html\">");
out.println("<img src=\"/examples/images/code.gif\" height=24 " +
"width=24 align=right border=0 alt=\"view code\"></a>");
out.println("<a href=\"/examples/servlets/index.html\">");
out.println("<img src=\"/examples/images/return.gif\" height=24 " +
"width=24 align=right border=0 alt=\"return\"></a>");
out.println("<h1>" + title + "</h1>");
out.println("</body>");
out.println("</html>");
}
}


The only difference I can see is the ResourceBundle class. What is it? Is
that what's causing my problem? I tried putting it on my code and I'm
getting the same error. Why does my first code works on JRun and not
Tomcat? Are there differences between these 2 webservers as far as these
HelloWorld programs are concerned. Thanks for your help.

Struggling Newbie,

-Ray


Gilli Julien

unread,
May 15, 2001, 2:54:58 AM5/15/01
to

> This code (above) runs perfectly on my JRun server. When I try it on my
> Tomcat I get an error 500 (see below):
>
> Internal Servlet Error:
>
> java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld)
> at java.lang.ClassLoader.defineClass0(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
> at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)

Did you save the source of the class in HelloWorld.java, be aware that the
class-loader is case sensitive; so you must respect the case.
If you've already done that, does your web.xml map the servlert correctly
(i.e with respect to the case etc..) ?

Just my 2 cents...
Keep us informed.

Julien Gilli.


Jon Skeet

unread,
May 15, 2001, 3:35:46 AM5/15/01
to
R@yZor <rfe...@earthlink.net> wrote:
> Here is the simplest possible servlet from the Core Servlets book from Sun
> by Marty Hall:
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class HelloWorld extends HttpServlet {
> public void doGet(HttpServletRequest request, HttpServletResponse
> response)
> throws ServletException, IOException {
> response.setContentType("text/html");
> PrintWriter out=response.getWriter();
> out.println("Hello World");
> }
> }
>
>
>
> This code (above) runs perfectly on my JRun server. When I try it on my
> Tomcat I get an error 500 (see below):
>
> Internal Servlet Error:
>
> java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld)
> at java.lang.ClassLoader.defineClass0(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
> at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
>
> -----rest of error here.......
>
> You might be thinking I don't have the class file on the correct directory
> but another hello world example from Tomcat is working perfectly on the same
> directory.

<snip>

No, I'm thinking that you've transferred the class without keeping the
case. My guess is that you've got a helloworld.class file rather than a
HelloWorld.class file.

Alternatively, you may have had a helloworld.java file (and possibly had
it declaring class helloworld) then tried accessing it as HelloWorld.

Basically, be careful of case with Java - it's very case sensitive.

--
Jon Skeet - sk...@pobox.com
http://www.pobox.com/~skeet
If replying to the group, please don't mail me at the same time

Ray F

unread,
May 15, 2001, 12:40:23 PM5/15/01
to
I saw that "helloworld" on the error and I have confirmed that the case is
correct. My HelloWorld file is HelloWorld.java and HelloWorld.class. Is it
possible that somewhere in my configuration it is converting it to
lowercase? But the Tomcat version works??!!

I am running Apache/Tomcat on Win98.

-Ray


R@yZor <rfe...@earthlink.net> wrote in message
news:6l3M6.4098$Az.4...@newsread2.prod.itd.earthlink.net...

Jon Skeet

unread,
May 15, 2001, 1:00:21 PM5/15/01
to
Ray F <ramon.m...@disney.com> wrote:
> I saw that "helloworld" on the error and I have confirmed that the case is
> correct. My HelloWorld file is HelloWorld.java and HelloWorld.class. Is it
> possible that somewhere in my configuration it is converting it to
> lowercase? But the Tomcat version works??!!

Where do you have the servlet configured? Is it in web.xml?

Ray F

unread,
May 15, 2001, 1:56:34 PM5/15/01
to
I'm not sure what you mean. My tomcat configuration is basically out of the
box. I didn't have to do any special configuration on server.xml or web.xml
file.

I will try converting my java file and class file to lowercase if in fact it
is somehow converting it to lowercase (the error 'helloworld').

Thanks,

-Ray


Jon Skeet <sk...@pobox.com> wrote in message
news:MPG.156b7105e...@mrmog.peramon.com...

Jon Skeet

unread,
May 15, 2001, 5:46:03 PM5/15/01
to
Ray F <ramon.m...@disney.com> wrote:
> I'm not sure what you mean. My tomcat configuration is basically out of the
> box. I didn't have to do any special configuration on server.xml or web.xml
> file.
>
> I will try converting my java file and class file to lowercase if in fact it
> is somehow converting it to lowercase (the error 'helloworld').

I've just thought of what it *might* be - how did you try to access it?
Could *that* be where you were using lowercase - in the URL?

Robbie Baldock

unread,
May 15, 2001, 6:13:07 PM5/15/01
to
On Tue, 15 May 2001 05:42:26 GMT, "R@yZor" <rfe...@earthlink.net>
wrote:

>Here is the simplest possible servlet from the Core Servlets book from Sun
>by Marty Hall:
>
>import java.io.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>
>public class HelloWorld extends HttpServlet {
> public void doGet(HttpServletRequest request, HttpServletResponse
>response)
> throws ServletException, IOException {
> response.setContentType("text/html");
> PrintWriter out=response.getWriter();
> out.println("Hello World");
> }
>}

Well for a start you won't see anything untli you do an out.close();


Robbie


-------------------------------------------------------------
Robbie Baldock
r c b AT e a s y n e t DOT c o DOT u k (you know what to do!)
http://www.rcb.easynet.co.uk/
-------------------------------------------------------------

R@yZor

unread,
May 16, 2001, 12:10:56 AM5/16/01
to
Ok here's an update on my progress:

On the examples folder (c:\tomcat\webapps\examples\WEB-INF\classes) where
Tomcat examples are working but mine won't. I tried renaming my
"HelloWorld" to Tomcat's "HelloWorldExample" and it works!! So somewhere it
is configured to look for "specific" file names. I checked the examples
folder (same as above) and I found 3 "properties" file.
LocalStrings.properties, LocalString_en.properties, and
LocalString_es.properties. I couldn't find any reference where a filename
may have been hard-coded. But I know that this is what the "ResourceBundle"
class is using. I think it's for different language support. "en" for
english and "es" for spanish. Anyway, I still don't know why my own
HelloWorld wouldn't run. At this point I don't care because it's just the
example and there's a configuration there somewhere for it I'm sure.

Now to my real servlet folder (c:\tomcat\webapps\ROOT\WEB-INF\classes).

The same HelloWorld program only works if I put it in a package. So I
create a folder under \classes\ called "mypackage" and add the line "package
mypackage;" to my java file. If I don't I get the error:

Location: /servlet/helloworld
Internal Servlet Error:

java.lang.IllegalAccessException: helloworld
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at
org.apache.tomcat.core.ServletWrapper.loadServlet(ServletWrapper.java:268)
at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:289)
at org.apache.tomcat.core.Handler.service(Handler.java:254)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:79
7)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpC
onnectionHandler.java:210)
at
org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at
org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)

On the same folder (under \classes\) there is a java servlet file called
SnoopServlet and this one works. So why is it that I need to have my
servlets in a package? Is this how Tomcat is suppose to work out-of-box
without configuration? It doesn't make sense!

-Ray


"Jon Skeet" <sk...@pobox.com> wrote in message

news:MPG.156bb3f2c...@mrmog.peramon.com...

Jon Skeet

unread,
May 16, 2001, 2:10:56 AM5/16/01
to
R@yZor <rfe...@earthlink.net> wrote:

> Location: /servlet/helloworld

This looks like you *are* using a URL with just helloworld instead of
HelloWorld. Try /servlet/HelloWorld instead - use the same case
*everywhere*, as I said before.

0 new messages