• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stuck on HelloWorldServlet

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm brand new to servlets and JSP, and I'm stuck.

I've installed Tomcat 5.0.28 (on a Mac running OS 10.4.1). I believe it's installed correctly: When I start Tomcat from Terminal, then use a browser (Firefox 1.0.4) to navigate to http://localhost:8080/ the Tomcat index page displays correctly. In addition, the packaged Tomcat servlet examples also work -- for example, http://localhost:8080/servlets-examples/servlet/HelloWorldExample

However, I can't get my own HelloWorldServlet to work. When I try to access http://localhost:8080/scwcd01/servlet/Hello I get an HTTP Status 404 message saying that "The requested resource (/scwcd01/servlet/Hello) is not available." I've tried numerous variations in my xml file (based on my book, as well as different web turorials), but nothing is working.

Under the webapps directory (the same one that contains servlets-examples), I have a directory called scwcp01. Under this directory, I have a web.xml file with the following content...

(I'm new to xml too.)

Also under scwcd01, I have directories WEB-INF/classes, which contains my compiled HelloWorldServlet.class. The java code is below...

What am I missing?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: The book I'm using is SCWCD Exam Study Kit (brand new 2nd edition, 2005) by Deshmukh, Malavia, Scharpino. Unlike what I've found in web tutorials, their example uses the method "service" rather than "doGet," and they omit the port 8080 designation from their URL. Unfortunately, they don't explain any of this -- not even in their "introductory chapters" for programmers with no servlet/JSP experience -- and all I get is a "connection refused" error. Outdated tutorials on the web get me farther along than that.

So can anyone recommend a better book?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your web.xml file must be in the WEB-INF folder.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So can anyone recommend a better book?


"Core Servlets and Java Server Pages" by Marty Hall.
Prentis is the publisher.

You can get the first edition (which is getting a bit outdated) for free here:
http://pdf.coreservlets.com
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
Your web.xml file must be in the WEB-INF folder.


Thanks Bear! I can't believe I missed that. Then again, there are so many new details here, I kind of expected something "obvious" like that.

Unfortunately, it's still not working. There must be at least one more detail I'm missing...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at Ben's 'Simple Servlet' site and compare your app structure to one of his. That should give you a clue as to what else is missing or misplaced.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I've tried to match my example to a modified version from Ben's Simple Servlet Examples. His works -- mine still doesn't. Here's what I have now under Tomcat's webapps directory...

Under "test/WEB-INF" a web.xml file containing:

Under "scwcd01/WEB-INF" a web.xml file containing:

I've also recompiled the java files. The class file under "test/WEB-INF/classes" is compiled from...

And the class file under "scwcd01/WEB-INF/classes" is compiled from...

This works...
http://localhost:8080/test/simple-servlet
...but this doesn't...
http://localhost:8080/scwcd01/Hello

On mine, I still get an HTTP Status 404 error with the description, "The requested resource (/scwcd01/Hello) is not available."
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I've isolated the problem, but I don't understand it.

When I copy the functioning xml file into my own directory, then change the values (and get rid of the non-functioning xml file), my servlet seems to work -- even though these values appear to be identical to those in the xml file I created.

When I paste the content of these xml files into MS Word and turn on the paragraph marks, then it appears that the functioning version has extra paragraph marks after each line (which I first noticed when I pasted the examples into the above post). But the odd thing is that these double paragraph marks don't show at all when I edit the files in Apple's TextEdit.

Why am I not seeing these paragraph marks in TextEdit? Isn't XML simple text? Should I be using a different tool to create/edit xml files? Or am I on the wrong track with this and missing something else instead?
[ June 11, 2005: Message edited by: marc weber ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there is problem with your web.xml file, you should see errors in the tomcat logs. Have you checked them?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
If there is problem with your web.xml file, you should see errors in the tomcat logs. Have you checked them?


That's interesting; I didn't know there were any logs. I'll look into this tomorrow.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi marc,
your deployement (file and directory structure in Tomcat) is wrong, and
your web.xml too.
File and structure in Tomcat (or Resin) (case sensitive):

yourTomcat_Root_Directory/webapp/scwcp01
\-----WEB-INF
| web.xml
|
\-----classes
HelloWorldServlet.class

<!-- End of the deployement -->

<!-- Source Code -->

<!-- Begin web.xml Begin -->

<?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>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/servlet/Hello</url-pattern>
</servlet-mapping>

</web-app>

<!-- End web.xml End -->

<!-- Begin HelloWorldServlet.java Begin -->
your code
<!-- End HelloWorldServlet.java End -->

deploy your web-app, start Tomcat, and test the page:

http://localhost:8080/scwcp01/servlet/Hello

enjoy tomcat
ser

p.s. the Tomcat logs are in
yourTomcat_Root_Directory/logs
\-----| ...
| localhost_log.2005-05-26.txt
localhost_log.2005-05-27.txt
...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A note on sergio's post: be sure that all your servlets are in a package other than the default.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help, but now I'm just more confused.

sergio: Now that I have web.xml under the WEB-INF directory (as Bear pointed out), I don't see a difference between what you posted and what I'm using, except that your url-pattern is "/servlet/Hello" rather than "/Hello". When I changed mine to "/servlet/Hello" that actually stopped it from working (yes, I'm restarting Tomcat each time and adjusting the URL as appropriate).



Aside from the url-pattern curiosity, I still have the problem of 2 seemingly identical xml files -- one of which works, while the other doesn't. I studied the log, but am in no position to interpret it. (I searched for "xml" but it doesn't appear, and "web" only appears in the context of "webapp".)

Now, I realize that I have a lot of learning ahead of me (such as logging) -- but I'm hesitant to move on to more advanced topics before getting a "simple" HelloWorld servlet to work consistently. So my dilemma is whether to keep banging my head with this example, or to move on with the intention of back-filling the details I'm missing.

Bear: I understand that using the default package is generally a "bad idea," but can it actually prevent a servlet from working in Tomcat? (I'm trying to eliminate as many variables as possible to isolate what's going wrong -- and other than Ben's, none of the examples I've seen use packages.)
[ June 13, 2005: Message edited by: marc weber ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but can it actually prevent a servlet from working in Tomcat?



Yes. In most cases, the class will not load if it is in the default package.

I would also drop the "servlet/" prefix to your mappings. It's non-standard and likely to be confused with the invoker (if you don't know what that is, don't worry about it yet).
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just as another point of reference, I set up a simple app that consisted of a single 'HelloServlet' servlet. It worked on Tomcat 5.0.25 without a hitch.

Here's the web.xml:



Note: the servlet is in a package. This is a must.

Note: no fancy embellishments to the servlet mapping.

Try this and see if you can get it to work. Feel free to change the package naming (in fact, please do), but be sure not to use the default package.

Btw, here's the servlet itself:



The entire web app consists of two files:

WEB-INF/web.xml
WEB-INF/classes/org/bibeault/hello/HelloServlet.class
[ June 13, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, I realize that I have a lot of learning ahead of me (such as logging) -- but I'm hesitant to move on to more advanced topics before getting a "simple" HelloWorld servlet to work consistently. So my dilemma is whether to keep banging my head with this example, or to move on with the intention of back-filling the details I'm missing.




I don't see "Hello, World" (HW) and logging as two separate issues.
The point of HW make it through the full 'write, compile, deploy, debug' cycle with as simple an app as possible. You don't want to move to a more complicated program until you're comfortable debugging the environment itself.

Look in TOMCAT_HOME/logs and see what files are in there.
You may want to delete all of them, restart Tomcat, and hit your app once.

If there is a problem with web.xml you will see logging statements that will help you to pinpoint the problem.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Ben. Moving on before you've got this basic stuff working would just result in more flat spots on your head.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
I agree with Ben. Moving on before you've got this basic stuff working would just result in more flat spots on your head.


Yeah, that's what I'm afraid of. Unfortunately, my book -- which is supposed to be for beginners -- doesn't mention any sort of troubleshooting ("log" isn't even in the book's index). Instead, the book states that "the details of this code will become clear as we move through the chapters," so apparently even the authors themselves don't expect readers to understand this exercise. The chapter devotes only a page and a half to writing, compiling, and deploying this servlet. Nothing on debugging.

Meanwhile, I've created an all-new attempt from scratch: Brand new directory with all new files. This works perfectly, with minor modifications (like the url-pattern) producing the expected results. But I still can't figure out why my first example doesn't work with the original xml file.

Thanks for the code, Bear! I will experiment with it later tonight.
[ June 13, 2005: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
... If there is a problem with web.xml you will see logging statements that will help you to pinpoint the problem.


Here's what I get in the log file when I start Tomcat and attempt to access http://localhost:8080/scwcd01/Hello using the error-producing xml file (line breaks inserted for posting)...

And here's what I get using the functioning xml file...

I don't see any difference in the logs of these attempts -- nor do I see any difference in the xml files themselves when compared in TextEdit. Yet one prevents the servlet from working.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both logs look fine. Parse errors in web.xml are usually very long and verbose.

It sounds like the app just never deployed.
The only difference between the working and-non working versions is the web.xml file?

I wouldn't get too hung up on it at this point. You've got two "hello, world" apps running. Save the file for a day when you've nothing to do and move forward.

By the way: Exam study guides are usually good at what the title suggests, getting you ready for an exam. They're generally not the best books for learning a technology from the begining. The one I recommended (as well as it's more up to date version "More Servlets and Java Server Pages) is excellent. For example, it tells you NOT to override the service method directly but to override doGet or doPost and then goes on to tell you exactly why. There are others as well. Do a search on "books" in either the JSP or Servlet forumns on this page and you will see lists of books that people have used and liked.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:
... The only difference between the working and-non working versions is the web.xml file? ...


Yes, this is why it's so strange. Probably just my luck that the first example I try has a file that's corrupt in some strange way that I'll never see again (if that makes sense).

Thanks for the book suggestion! You're right about certification books -- I couldn't have learned basic Java from one. I seem to do best working between two or more books at a time anyway, using one as a primary resource but supplementing it with another author for topics that don't make sense.
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have typo in your scwd01/WEB-INF/web.xml, you mispelled "servelt-class". The Tomcat log should have displayed an error message stating that your web.xml was not valid. Therefore your Servlet(HelloWorldServlet.class) was never defined.

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Craig Jackson:
You have typo in your scwd01/WEB-INF/web.xml, you mispelled "servelt-class"...


Thank you!

I can't tell you how many times my eyes must have passed over that without noticing. Strange (apparently) that Tomcat didn't log anything, but I'll sleep better tonight knowing what the problem was!
[ June 13, 2005: Message edited by: marc weber ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many eyes have missed it. Good catch Craig.

You might want to put that spelling error back in the web.xml file and try to deploy the app again. Then, check the logs. I've never seen Tomcat not log these types of errors.

Because OSX is built on top of Unix, you've got some nice tools available to you. One cool one is the tail program.
CD into the TOMCAT_HOME/logs directory and type:
tail -f *

Then start Tomcat. The tail program will keep printing the contents of your log files (the tail end of them) to the console as they are updated. This allows you to watch your logs in real time as you are running the application.

Another that would have helped you with this issue is the "diff" program which compares two files and prints out the differences between them.
Type: "man diff" for more information.
[ June 14, 2005: Message edited by: Ben Souther ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, I double (triple, quadruple) checked the web-app parameters character by character, because that's the part I wasn't familiar with. I even replaced all my zeros to ensure they weren't uppercase "O's." But when it came to a common word... Well, that's how errors happen.

Thanks for the tips! The tail program sounds especially useful.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The tail program sounds especially useful.



Indeed it is.

You may want to grab one of the "Unix for OS X" books. The Unix command line is a powerful tool (or more correctly, the means to use powerful tools) that is of great use to us developing on OS X (or other Unix platforms).
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I foolishly played with a G5 box at Microcenter the other day.
I can tell that is going to end up being a very expensive mistake. :-)
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
... You may want to grab one of the "Unix for OS X" books...


Yes, I'm debating between Learning Unix for Mac OS X Tiger by Dave Taylor, or The Mac OS X Command Line: Unix Under the Hood by Kirk McElhearn.

Taylor's is geared (or at least marketed) towards Tiger, but McElhearn's looks more comprehensive. Either one would probably be a good reference for me.

I've also downloaded a couple of chapters from the Marty Hall book that Ben recommended above (Core Servlets and Java Server Pages). If I find those helpful, then I'll probably order the newer edition.
[ June 14, 2005: Message edited by: marc weber ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic