Wednesday, June 18, 2008

What's Servlet Mapping? What's Servlet Invoker?


What's Servlet Mapping? What's Servlet Invoker?

Servlet Mapping & it's uses

This mapping is specified in the Deployment Descriptor (web.xml) of a Web Application to specify which servlet to be called for an incoming URL. The entry of the <servlet-mapping> element of the web.xml contains two child elements - <servlet-name> and <url-pattern>.

<servlet-name> element is used to specify the name of the servlet, which should be called for an incoming URL matching the pattern specified as the value of the <utl-pattern> element. Remember that the actual class file for the servlet is already specified in the <servlet> entry for that particular servlet. Every <servlet> element will have at least two child elements - <servlet-name> and <servlet-class>. In addition it may contain initialization parameters.

Example: typical entries of <servlet> and <servlet-mapping> may look like:-

...
<servlet>
<servlet-name>NameOfTheServlet</servlet-name>
<servlet-class>Fully Qualifies Class Name of the Servlet</servlet-class>
</servlet>
...

<servlet-mapping>
<servlet-name>NameOfTheServlet</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
...

Possible alternative of Servlet Mapping - Servlet Invoker

Servlet Invoker is a mechanism which allows any servlet to be called by directly specifying the servlet class. It's an implementation dependent feature i.e., every Web Container may have a different way of supporting this functionality.

It's highly discouraged to use this feature as it may result into a security breach. By enabling this mechanism every servlet specified in the CLASSPATH of the Web Container (even those which are kept inside JAR files) can be directly called from outside. For development and testing environments, you may give it a try; but, for production environments the Servlet Invoker mechanism should never be used for calling servlets.

Servlet Mapping is a much better, robust, secure, and maintainable way of specifying which servlet to be called for an incoming URL. It makes the call transparent from the client's point of view. If we come up with a different version of the servlet (or if we want to change the servlet altogether) then we just need to modify the relevant entries in the Deployment Descriptor (web.xml). This change can be completely transparent to the clients as the URL may still the remain the same and the Web Container will simply call the modified servlet (or the newly specified servlet) for that URL.



Share/Save/Bookmark