The <servlet-class> element specifies the fully qualified class name of the Java class that implements the servlet functionality.
This element tells the web container which class to instantiate when the servlet needs to be loaded and initialized.
The class must be available in the web application's classpath, typically within the WEB-INF/classes directory or packaged in JAR files within WEB-INF/lib.
The web container uses reflection to load and instantiate the specified class during servlet initialization.
This process occurs either at application startup (if load-on-startup is specified) or upon the first request to the servlet.
The container maintains the servlet instance throughout the application's lifecycle, following the servlet specification's lifecycle management rules.
Naming Requirements:
- The package name components must be separated by dots (
com.mtitek.servlets.test1.
).
- The class name must not include the
.class
extension.
- The fully qualified name must match exactly with the compiled class file location.
- The class must be accessible through the web application's classloader.
<servlet>
<servlet-name>MyServlet1Name1</servlet-name>
<servlet-class>com.mtitek.servlets.test1.MyServlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>MyServlet1Name2</servlet-name>
<servlet-class>com.mtitek.servlets.test1.MyServlet1</servlet-class>
</servlet>
The example demonstrates that the value of the <servlet-class> element does not need to be unique across servlet declarations.
The same class "com.mtitek.servlets.test1.MyServlet1" is associated with two different logical names (MyServlet1Name1 & MyServlet1Name2).
This configuration pattern enables several advanced deployment scenarios.
Multiple Instance Benefits:
- Instance Isolation: Forces the web container to create separate instances of the servlet, ensuring complete isolation between different configurations.
This prevents shared state issues that might occur if the container reused the same instance.
- Configuration Variants: Allows providing different initialization parameters to the same servlet class, enabling different behavioral modes or configurations for the same underlying implementation.
- Load Distribution: Can be used in conjunction with different URL mappings to distribute load or provide specialized entry points for the same servlet functionality.
The
getServletName()
method can be used within the servlet implementation to determine which configuration instance is currently executing, allowing for conditional logic based on the servlet name.
Implementation Requirements:
The specified class must extend
HttpServlet
(for HTTP-specific servlets) or
GenericServlet
(for protocol-independent servlets),
and ultimately implement the
jakarta.servlet.Servlet
interface.
Failure to meet this requirement will result in a ClassCastException during servlet loading:
HTTP Status 500 -
java.lang.ClassCastException: com.mtitek.servlets.test1.MyServlet1 cannot be cast to jakarta.servlet.Servlet