Servlet Interview Questions and Answers

This section covers a range of Servlet interview questions suitable for both beginners and experienced professionals.

1. Number of Servlet Objects Created.

Only one servlet object is created per web application when the first request arrives.

2. Servlet Lifecycle.

  1. Loading: The servlet class is loaded into memory.
  2. Instantiation: A servlet instance is created.
  3. Initialization: The init() method is called.
  4. Request Handling: The service() method handles each incoming request.
  5. Destruction: The destroy() method is called when the servlet is unloaded.

3. Servlet Lifecycle Methods.

Method Description
init(ServletConfig config) Called once when the servlet is first loaded. Used for initialization.
service(ServletRequest request, ServletResponse response) Called for each client request. Handles the request and sends a response.
destroy() Called once before the servlet is unloaded. Used for cleanup.

4. Who Creates the Servlet Object?

The web container (servlet container).

5. When is the Servlet Object Created?

When the first request for that servlet arrives.

6. GET vs. POST Methods.

Feature GET POST
Data Transmission Limited data, sent in the URL Larger data, sent in the request body
Security Less secure More secure
Bookmarking Can be bookmarked Cannot be bookmarked
Idempotency Idempotent (multiple requests have the same effect) Not idempotent

7. PrintWriter vs. ServletOutputStream.

PrintWriter is a character stream; ServletOutputStream is a byte stream. PrintWriter is generally preferred for text-based responses; ServletOutputStream is more flexible for handling binary data.

8. GenericServlet vs. HttpServlet.

GenericServlet is protocol-independent. HttpServlet is specific to HTTP and provides additional methods for handling HTTP requests (GET, POST, etc.).

9. Servlet Collaboration.

Servlet collaboration involves communication between servlets. Methods include using the RequestDispatcher interface and the sendRedirect() method.

10. RequestDispatcher Interface.

The RequestDispatcher interface forwards requests to other resources (JSPs, servlets, etc.) or includes content from another resource.

11. Calling a JSP from a Servlet.

Use the RequestDispatcher:

Example

RequestDispatcher rd = request.getRequestDispatcher("/path/to/jsp");
rd.forward(request, response);

12. forward() vs. sendRedirect().

Method Behavior
forward() Forwards the request to another resource on the server
sendRedirect() Sends a redirect response to the client; the client then makes a new request

13. ServletConfig vs. ServletContext.

ServletConfig is created per servlet instance. ServletContext is created per web application.

14. Session Tracking.

Session tracking maintains a user's state across multiple requests (since HTTP is stateless). Methods include using cookies, HttpSession, and URL rewriting.

15. What are Cookies?

Cookies are small pieces of data stored on the client's browser to maintain state across multiple requests. They can be used for session management and personalization.

16. Cookies vs. HttpSession.

Cookies are client-side; HttpSession is server-side.

17. Filters.

Filters intercept and process requests or responses. They can perform tasks like authentication, logging, and data compression.

18. Actions During Project Deployment.

Use the ServletContextListener interface.

19. Disadvantage of Cookies.

Cookies can be disabled by users, potentially breaking functionality that relies on them.

20. File Uploads in Servlets.

(This section would discuss how to handle file uploads in servlets, often involving libraries like Apache Commons FileUpload.)

21. load-on-startup in Servlets.

The load-on-startup element in web.xml specifies the order in which servlets should be loaded when the application starts. A value of 0 or greater loads the servlet when the application starts, improving initial response time.

22. Negative Value for load-on-startup.

A negative value or omission has no effect; the servlet is loaded on the first request.

23. WAR (Web Archive) Files.

A WAR file packages a web application into a single deployable unit.

24. Creating a WAR File.

Use the jar tool or an IDE (like Eclipse or NetBeans).

25. Annotations in Servlet 3.

  • @WebServlet: For servlets
  • @WebListener: For listeners
  • @WebFilter: For filters

26. ServletContextEvent.

Fired when the application starts or stops.

27. HttpSessionEvent.

Fired when a session is created or destroyed.

28. ServletContextAttributeEvent.

Fired when attributes are added, removed, or replaced in the application scope.

29. welcome-file-list.

Specifies the default pages to serve when a user requests the application's root directory.

30. Attributes in Servlets.

Attributes (using setAttribute(), getAttribute(), removeAttribute()) are used to share data between different parts of a web application (request, session, application scope).