File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Problem with invoking a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with invoking a servlet" Watch "Problem with invoking a servlet" New topic

Problem with invoking a servlet

Dave Thomas
Ranch Hand

Joined: Nov 05, 2001
Posts: 30
Do I have to declare every servlet explicitly in web.xml. I am currently trying to invoke it using the url: http://localhost:8082/mytest/servlet/TestServlet
and i am getting the old 404 page. It works fine however if i use a mapping in web.xml. I must have done something wrong somewhere.
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
You can use the default mapping of a servlet but it must be in a package. So if you have a servlet my.test.TestServlet in an app deployed at /mytest, you can access it using this url (I'm not sure if it's '/mytest/servlet/' or '/mytest/servlets/':
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hi Dave
Yup, you pretty much have to declare all your servlets in the deployment descriptor. If not, the servlet container won't know which servlet classes to load and instantiate (which happens when the container is fired up)
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
You DON'T have to define every servlet in the web.xml file. This definition is needed only :
1. If inside the servlet class you use some logic to use the ServletConfig.getInitParameter(), which has to get the init-param from the web.xml file.
2. If you don't want to call the servlet by its complete package hierarchy and want to give it a short name. This you do inside the <servlet> .. </servlet> element.
3. If you want to override the calling url-pattern. This you do inside the <servlet-mapping> .. </servlet-mapping> element.
For example, I have a servlet class file "HelloHttp.class" in the webapps/docroot/WEB-INF/classes/mypkg directory. I can access it by http://localhost:8080/docroot/servlet/mypkg.HelloHttp without even defining it in the web.xml file.
However, if I'm calling it this way, then the ServletConfig.getInitParameter() will return null.
If I take the trouble and define it in the web.xml file as
<servlet-name> someservlet </servlet-name>
<servlet-class> mypkg.HelloHttp </servlet-class>
then I will be able to call this servlet as
http://localhost:8080/docroot/servlet/someservlet and getInitParameter() will return 30.
However again, calling the same servlet as http://localhost:8080/docroot/servlet/mypkg.HelloHttp will result in getInitParameter() returning null (Since the ServletConfig is not getting used here).
I hope this is clear.
Thanks, Sudd

Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hmm, interesting. I thought every servlet had to be defined in the web-inf file. Thanks for this bit of information.
However, I can't really think of a good reason why you wouldn't want to define your servlet in the web-inf file, particularly (as Sudd mentioned) if it restricts your access to things such as initialisation parameters.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Also.. if you're using Tomcat 4.1.12 and above, you must *uncomment* the invoker servlet. It used to work out of the box, but now if you want to invoke servlets without mapping (using /servlet) you must enable this.
Sudd Ghosh
Ranch Hand

Joined: Oct 23, 2002
Posts: 187
I agree with Mike. It is the "invoker" servlet in Tomcat in the conf/web.xml file that maps /servlet/* to any servlet that we put inside the servlet class. However, even one can override this mapping for any servlet using the <servlet-mapping> and <url-pattern> elements.
This whole mapping business is expalined very well in Martin Hall's book : More Servlets and JSP
Thanks, Sudd
I agree. Here's the link:
subject: Problem with invoking a servlet
jQuery in Action, 3rd edition