wood burning stoves 2.0*
The moose likes Servlets and the fly likes My first servlet won't go Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "My first servlet won Watch "My first servlet won New topic
Author

My first servlet won't go

Anthony Denahy
Greenhorn

Joined: Jan 13, 2006
Posts: 7
I'm doing a Grad Dip in IT, but it doesn't cover J2EE so I want to teach myself. I borrowed "Java for the Web with Servlets,JSP, and EJB" and thought, "Beaut, I'll just work my way through this and I'll have a good basic understanding of J2EE". Unfortunately, even though I have got Tomcat 4.1 working (I decided to use that since the book is 2002), for some unknown reason, when I try to call my servlet from Firefox (localhost:8080 etc) I get an error message: "The requested resource (/myApp/servlet/TestingServlet) is not available". This is the servlet code:

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>");
}
}

I must have everything set up OK as the example servlets that come with Tomcat work fine. Any ideas?
Sara Tracy
Ranch Hand

Joined: Jan 06, 2006
Posts: 45
Make sure that your directory structure is right. And have you set your classpath right? And also you should have a web.xml. You might have missed out doing one of these.
Anthony Denahy
Greenhorn

Joined: Jan 13, 2006
Posts: 7
Thankyou for those suggestions Sara - I think I have all those things OK (sorry, I should have taken more effort to give the complete picture).

What I'll try to do is use another simple beginners tutorial, with Tomcat 5.5, and see if it works then.

Anthony
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It is also worth pointing out that the example you are following is using the InvokerServlet.

When you write a Servlet, you need to map it to a 'name' on the server so that you can then ask for it by that name. This requires configuration in the web.xml file. The InvokerServlet short cuts this process by assuming that the servlet name is the same as the name used on the server. But there are several issues with using the InvokerServlet and it has fallen into disfavour - we don't recommend it, even for learning, and we have a page on the wiki devoted to it.
Anthony Denahy
Greenhorn

Joined: Jan 13, 2006
Posts: 7
Thanks David. It is not an invoker issue - doesn't work either way. (?? P.S. I think I may have not completely uncommented the invoker)
[ January 14, 2006: Message edited by: Anthony Denahy ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Anthony Denahy:
Thankyou for those suggestions Sara - I think I have all those things OK (sorry, I should have taken more effort to give the complete picture).
What I'll try to do is use another simple beginners tutorial, with Tomcat 5.5, and see if it works then.
Anthony


If you had all those things set up OK, then it would be working.

Where is your servlet's class file?
What is the URL you are using to invoke the servlet?
Did you explicitly map the servlet or are you relying on the InvokerServlet?
If you did explicitly map it, post your servlet and servlet-mapping entries from your web.xml file.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
How are you starting Tomcat?
Have you examined the log files?
Bill
Anthony Denahy
Greenhorn

Joined: Jan 13, 2006
Posts: 7
Progress! I can get servlets to work in my "myApp" directory, however, at present I need to use the actual class name - for some reason I cannot use the name mapped in web.xml, and Invoker is not on (I think).

Thankyou everyone - Bill, David, Ben. I have just read the section on asking good questions too - useful advice
[ January 14, 2006: Message edited by: Anthony Denahy ]
Anthony Denahy
Greenhorn

Joined: Jan 13, 2006
Posts: 7
All fixed! The problem was in the Deployment Descriptor. The book I was using gave this as a suitable Descriptor for my first servlet:


but it seems that this is not enough - the following must be included also:



where the url-pattern can be a variety of things e.g. instead of "/servlet/Testing" I could use just "/Test", or if I wanted to be very tricky, even "/rabbit/wombat/walrus", in which case, if my TestingServlet.class was in the myApps/classes, then in my browser I would need to use the URL: http://localhost:8080/rabbit/wombat/walrus in order to run the servlet!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My first servlet won't go