• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with invoking a servlet

 
Dave Thomas
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic