Nearby lessons
6 of 34Servlet - GenericServlet
- Understand GenericServlet abstract class
- Learn the 3 init() method override scenarios
- See complete working code examples
GenericServlet simplifies servlet development by implementing the Servlet interface and providing default implementations for all methods except service(). This lesson covers GenericServlet with complete code examples.
What is GenericServlet?
When implementing the Servlet interface directly, you must provide implementation for all 5 methods — whether required or not. This increases code length and reduces readability.
GenericServlet already implements the Servlet interface and provides implementations for all methods except service(). By extending GenericServlet, you only need to implement service(), reducing code length and improving readability.
GenericServlet is an abstract class that implements Servlet, ServletConfig, and Serializable interfaces.
FirstServlet.java extending GenericServlet
Folder structure: advapps1B/ → WEB-INF/ → classes/ → FirstServlet.class
Note: In this program, two classes are available: FirstServlet and GenericServlet. The web container creates an object for FirstServlet. When a method is called, the container first checks if our class contains that method. If yes, it's executed. If not, only then is the parent class method (GenericServlet) executed. First priority goes to our class because the object belongs to it.
Internal Implementation of GenericServlet
Case 1: If our servlet class does not contain any init() method:
Web container calls init(SC config) of GenericServlet. This saves the config object and internally calls no-arg init(). If our class has no no-arg init(), the GenericServlet no-arg init() (empty implementation) is called. getServletConfig() returns the config object.
Case 2: If we override init(SC) in our servlet class:
Web container calls our init(SC) method. This approach is not recommended because the config object is not saved for future use. getServletConfig() returns null.
Case 3: If we override no-arg init() (RECOMMENDED):
Web container calls GenericServlet's init(SC), which saves the config object and internally calls our no-arg init() method. getServletConfig() returns the config object.
| Scenario | Method Call Sequence | getServletConfig() |
|---|---|---|
| No init() method | GS:init(SC) → GS:init() | Returns config object |
| Override init(SC) | FS:init(SC) | Returns null |
| Override no-arg init() | GS:init(SC) → FS:init() | Returns config object |
Q: Why does GenericServlet have 2 init() methods?
- init(SC) is for the web container
- init() is for the programmer
Q: Which init() method is recommended to override?
no-arg init() method
Q: Why is the config variable declared as transient in GenericServlet?
For security — the config object should not be serialized across the network.
javax.servlet.http Package
This package contains classes and interfaces for developing HTTP-based servlets.
Important interfaces of javax.servlet.http:
- HttpServletRequest — child interface of ServletRequest, used to get end-user information
- HttpServletResponse — child interface of ServletResponse, used to prepare/send response
- HttpSession — used to implement session management
Important classes of javax.servlet.http:
- HttpServlet — child class of GenericServlet, base class for HTTP-based servlets
- Cookie — used in session management
- GenericServlet implements Servlet, ServletConfig, and Serializable
- Override no-arg init() — it's the recommended approach
- Config object is transient for security
- Exam-style questions at the end