Servlet interview questions

What is a Servlet?

A servlet is a Java technology and it is managed by a container called servlet engine. It generates dynamic content and interacts with client through Request and Response.

Why servlet is mostly used?

Servlets are mostly used because they are platform-independent Java classes and are compiled to platform-neutral byte code. Java byte code can be loaded dynamically into and run by java enabled web server.

What is called servlet container?

A servlet container is a part of Web server that provides network services depends on request and response are sent, MIME based requests and responses. It contains and manages servlets through their life cycle.

What is a filter?

A filter is nothing but a piece of code which can be reusable that will be transforming the content of HTTP requests, response and header information.

What is called a session?

A session is an object which is used by a servlet and it is used to track user interaction with a web application across multiple HTTP requests.

What is servlet mapping?

Servlet Mapping is an association mapping between servlet and a URL pattern. This is used to map servlets with the requests.

Which interface should be implemented by all servlets?

Servlet interface should be implemented by all servlets.

What is life cycle of Servlet?

Following is life cycle of Servlet:

  • Loaded
  • Initialized
  • Destroy
  • Unloaded

What is the difference between Servlet Request and Servlet Context when calling a Request Dispatcher?

Relative URL can be called when Servlet Request is used and Relative URL is not used when using Servlet Context.

What are the features added in Servlet 2.5?

Following are the features added in Servlet 2.5:

  • Dependency on J2SE 5.0
  • Support for annotations
  • Loading the class
  • Several web.xml
  • Removed restrictions
  • Edge case clarifications

When servlet is loaded?

A servlet can be loaded when:

  • First request is made
  • Auto loading and Server starts up
  • There is a single instance that answers all requests concurrently which ¬†saves memory
  • Administrator manually loads.

When Servlet is unloaded?

A servlet is unloaded when:

  • Server shuts down
  • Administrator manually unloads

What are the supporting protocol by HttpServlet ?

HttpServlet supports only HTTP and HTTPS protocol.

What is called Session Tracking?

Session tracking is used to maintain a state on the series of requests from the same user for a given period of time.

Why session tracking is needed?

Every HTTP request needs to be captured by HTTP protocol and for that, state is captured. Tracking of state is called session tracking.

What are the types of Session Tracking ?

There are following types of session tracking:

  • URL rewriting
  • Hidden Form Fields
  • Cookies
  • Secure Socket Layer (SSL)

What are the advantages of cookies?

Cookies are used to store long term information that can be maintained without server interaction. Small and Medium size data are kept in a queue.

What is URL rewriting?

URL rewriting is one of the methods of session tracking in which additional data is appended at the end of each URL. This additional data identifies the session.

What is servlet lazy loading?

A servlet container which does not initialize at the start up, this is known as servlet lazy loading.

What is Servlet Chaining?

Chaining is one of the methods where out of one servlet is given to the second servlet. This chaining can happen for any number of servlets.

What are the important functions of filters?

Following are the important functions of Filters:

  • Security check
  • Modifying the request or response
  • Data compression
  • Logging and auditing
  • Response compression

What are the functions of Servlet container?

Following are the functions of the Servlet container:

  • Lifecycle management
  • Communication support
  • Multithreading support
  • Declarative security
  • JSP support

What is the difference between JSP and Servlets ?

JSP supports HTTP protocol which mainly used for presentation. But a servlet can support any protocol like HTTP, FTP, SMTP etc.