• 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

My first servlet won't go

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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

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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you starting Tomcat?
Have you examined the log files?
Bill
 
Anthony Denahy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic