Nearby lessons

6 of 34

Servlet - GenericServlet

📌 What You Will Learn
  • 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.

Example02
JCode Cell
1 
2import javax.servlet.*;
3import java.io.*;
4import javax.servlet.annotation.*;
5@WebServlet("/test")
6public class FirstSevlet extends GenericServlet
7{
8public void service(ServletRequest req, ServletResponse resp) throws ServletException,IOException
9{
10PrintWriter out = resp.getWriter();
11out.println("<h1>Writing servlet by extending GS is very easy</h1>");
12}
13}
14
Output

<h1>Writing servlet by extending GS is very easy</h1>
      

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.

ScenarioMethod Call SequencegetServletConfig()
No init() methodGS: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.

Example03
JCode Cell
1 
2public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
3{
4private transient ServletConfig config;
5 
6public void init(ServletConfig config) throws ServletException
7{
8this.config = config;
9init();
10}
11 
12public void init() throws ServletException
13{
14}
15 
16public ServletConfig getServletConfig()
17{
18return config;
19}
20::::::::::::::::::::::::::::::::
21}
22

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
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3