Show TOC Start of Content Area

Object documentationInvoker Servlet  Locate the document in its SAP Library structure

Definition

The invoker servlet is implemented in the InvokerServlet class that is part of the J2EE Engine’s Web Container. It is declared in the global-web.xml descriptor. It is mapped to the /servlet/* pattern.

Use

The invoker servlet is used to invoke servlet classes that are available to the application classloader (these are classes located in the WEB-INF\classes, WEB-INF\lib, WEB-INF\additional-lib directories of the application). It can invoke servlets either by servlet name (as declared in the <servlet-name> tag of the web.xml), or by its fully-qualified class name (in this case the servlet is not necessary to be declared in the web.xml descriptor). This behavior is determined by the value of the InvokeByClassName initialization parameter defined for the invoker servlet:

·        If it has a value of true, then the invoker servlet tries to load the servlet by servlet name first. If it cannot find a match by the servlet name, it tries to find and load the servlet by its class name. This class name must appear after the /servlet/ pattern in the request URL. For example, http://host:port/servlet/mypackage.example.MyServlet invokes the myservlet that has the following declaration in the web.xml file:

<servlet>

   <servlet-name>myservlet</servlet-name>

   <servlet-class>mypackage.example.MyServlet</servlet-class>

</servlet>

Note

The invoker servlet checks whether the requested class is a servlet (that is, it must implement the javax.Servlet interface) when loading it by its class name. If this condition is not met, it will not load the class.

·        If it has value of false, then the invoker tries to load the requested servlet only by its servlet name declaration. If it finds no match, a “File not found” error message is returned to the client. So, the correct URL to invoke our example myservlet servlet is http://host:port/servlet/myservlet.

Caution

You must make sure you do not map any component to the /servlet/* pattern in the web.xml descriptor of your Web application, otherwise you will no longer be able to use the functions of the invoker servlet.

Notes on Invoking Servlets Using the Class Name

You should be aware that the Web Container loads and initializes all servlets that have been declared in the web.xml descriptor at Web application startup time with the corresponding initialization parameters configured (even if the load-on-startup attribute is missing or negative). You then need to call this servlet using either the URL mapping you have defined for it, or using its servlet name (that is, the name specified as the value of the servlet-name tag) in the URL in order to get this servlet instance (initialized with your specific initialization parameters, run-as identity, and security role references). However, if you request the servlet using its fully-qualified class name (that is, using the /servlet/<classname> URL pattern), you actually get a new instance that is initialized without any parameters. This instance is loaded on the first request to it.

Example

The following example attempts to illustrate the difference between calling servlets using the servlet name declaration and using the class name. Assume you have the test.example.ExampleInitServlet class in your application and you have declared a servlet name and some initialization parameters in the web.xml deployment descriptor as follows:

<servlet>

   <servlet-name>ExampleInitServlet</servlet-name>

   <servlet-class>test.example.ExampleInitServlet</servlet-class>

   <init-param>

      <param-name>testname</param-name>

      <param-value>Test</param-value>

      <description>

      </description>

   </init-param>

</servlet>

 

If the test.example.ExampleInitServlet servlet gets the value of the testname parameter and prints it on the screen, you will get the following results depending on the URL with which you request the servlet:

URL

Result

http://host:port/<contextRoot>/servlet/ExampleInitServlet

Test

http://host:port/<contextRoot>/servlet/test.example.ExampleInitServlet

Null

 

 

 

End of Content Area