Meaningless Drivel is fun!*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with invoking a servlet" Watch "Problem with invoking a servlet" New topic
Author

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.
Thanks
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/':
http://localhost:8082/mytest/servlet/my.test.TestServlet
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)
Mark.
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>
<servlet-name> someservlet </servlet-name>
<servlet-class> mypkg.HelloHttp </servlet-class>
<init-param>
<param-name>somename</param-name>
<param-value>30</param-value>
</init-param>
</servlet>
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


SCJP 1.4, SCWCD, SCBCD 1.3
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.
Mark.
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: http://aspose.com/file-tools
 
subject: Problem with invoking a servlet