All Questions


  1. What is a servlet? What is the architecture of the servlet package? What is the difference between HttpServlet and GenericServlet?
  2. List advantages of JSP over Servlet.
  3. What is a Scriptlet? What is an output comment?
  4. What is the difference between Get and Post method?
  5. What is Session Tracking? Write a program for implementing session tracking page hit counter using cookies?
  6. What is JDBC? List out all various types of JDBC Driver. Explain Thick and Thin driver.
  7. Explain difference between ServletConfig and ServletContext object.

  1. What is a servlet? What is the architecture of the servlet package? What is the difference between HttpServlet and GenericServlet?
    1. What is a Servlet?

      1. A Servlet is a Java class that is used to build dynamic web applications. It runs on a Java-enabled web server (like Apache Tomcat) and handles client requests and generates responses, typically over the HTTP protocol.

      2. Servlets are a core component of Java EE (Enterprise Edition) and are managed by a Servlet Container, which is part of a web server responsible for the lifecycle and mapping of servlets.

      3. Key Features of Servlets

        Feature Description
        Server-side Technology Executes on the server to generate dynamic content
        Protocol Support Primarily handles HTTP (via HttpServlet)
        Platform Independent Written in Java, hence cross-platform
        Persistent Servlet objects are instantiated once and reused
        Secure Java’s security features help mitigate threats
        Efficient Uses threads instead of creating new processes (unlike CGI)
    2. Servlet Architecture

      1. The Servlet architecture follows a request-response model where the client (browser) sends a request to the server, and the Servlet processes this request to return a response.

        image.png

      2. Servlet Architecture Components

        Component Description
        Client (Browser) Sends the HTTP request (GET/POST)
        Web Server Receives the request and passes it to the Servlet Container
        Servlet Container Loads the servlet, manages its lifecycle, and handles mapping
        Servlet Processes the request and returns the response
        Response The result (usually HTML or JSON) sent back to the client
      3. Servlet Request-Response Cycle

        1. Client Request: User sends a request to a servlet-based URL (e.g., /loginServlet).
        2. Servlet Container: Intercepts request and matches it to a servlet (via web.xml or annotations).
        3. Servlet Execution:
          1. Container calls init() if the servlet is not initialized.
          2. Then it invokes service() method which dispatches to doGet() or doPost().
        4. Response Generation: Servlet creates and sends the response using ServletResponse object.
        5. Client Receives Response: Browser displays the result.
      4. Servlet Lifecycle (Managed by Container)

        Phase Method Purpose
        Initialization init(ServletConfig) Initializes servlet instance (runs once)
        Request Handling service() Handles all requests (delegates to doGet() or doPost())
        Destruction destroy() Releases resources before servlet is removed
    3. Difference Between GenericServlet and HttpServlet

      1. GenericServlet and HttpServlet are two important classes in the javax.servlet package. GenericServlet is protocol-independent, while HttpServlet is specifically designed for handling HTTP requests.

        Feature GenericServlet HttpServlet
        Defined In javax.servlet.GenericServlet javax.servlet.http.HttpServlet
        Protocol Support Protocol-independent HTTP-specific
        Methods Provided Must override service(ServletRequest, ServletResponse) Provides doGet(), doPost(), doPut(), doDelete(), etc.
        Use Case For custom protocols (FTP, SMTP) or generic server applications For web-based applications using HTTP
        Subclass Responsibility Subclass must implement service() Subclass implements doGet(), doPost() depending on request type
        Session Support Not available Supports session management via HttpSession
    4. Code Example of GenericServlet

      public class MyGenericServlet extends GenericServlet {
          public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
              res.getWriter().println("This is a GenericServlet");
          }
      }
      
    5. Code Example of HttpServlet

      public class MyHttpServlet extends HttpServlet {
          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
              response.getWriter().println("This is an HttpServlet handling GET");
          }
      }