Servlet Interview Questions
Servlet Interview Questions
Servlet Interview Questions
2. A web server responsibility is to handler HTTP requests from client browsers and
respond with HTML response. A web server understands HTTP language and runs
on HTTP protocol.
3. Apache Web Server is kind of a web server and then we have specific containers
that can execute servlets and JSPs known as servlet container, for example
Tomcat.
4. Application Servers provide additional features such as Enterprise JavaBeans
support, JMS Messaging support, Transaction Management etc. So we can say that
Application server is a web server with additional functionalities to help developers
with enterprise applications.
6. A HTTP method is said to be idempotent if it returns the same result every time.
HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are idempotent method
and we should implement our application to make sure these methods always
return same result. HTTP method POST is non-idempotent method and we should
use post method when implementing something that changes with every request.
7. For example, to access an HTML page or image, we should use GET because it
will always return the same object but if we have to save customer information to
database, we should use POST method. Idempotent methods are also known as
safe methods and we don’t care about the repetitive request from the client for safe
methods.
10. The “Content-Type” response header is known as MIME Type. Server sends
MIME type to client to let them know the kind of data it’s sending. It helps client in
rendering the data for user. Some of the mostly used mime types are text/html, text/
xml, application/xml etc.
11. We can use ServletContext getMimeType() method to get the correct MIME type of
the file and use it to set the response content type. It’s very useful in downloading
file through servlet from server.
13. Web Applications are modules that run on server to provide both static and dynamic
content to the client browser. Apache web server supports PHP and we can create
web application using PHP. Java provides web application support through Servlets
and JSPs that can run in a servlet container and provide dynamic content to client
browser.
14. Java Web Applications are packaged as Web Archive (WAR) and it has a defined
structure like below image.
15.
18. Java Servlet is server side technologies to extend the capability of web servers by
providing support for dynamic response and data persistence.
19. The javax.servlet and javax.servlet.http packages provide interfaces and classes for
writing our own servlets.
20. All servlets must implement the javax.servlet.Servlet interface, which defines
servlet lifecycle methods. When implementing a generic service, we can extend
the GenericServlet class provided with the Java Servlet API. The HttpServlet class
provides methods, such as doGet() and doPost(), for handling HTTP-specific
services.
21. Most of the times, web applications are accessed using HTTP protocol and thats
why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below
image.
22.
27. Servlet containers are also known as web container, for example Tomcat. Some of
the important tasks of servlet container are:
1. Communication Support: Servlet Container provides easy way of
communication between web client (Browsers) and the servlets and
JSPs. Because of container, we don’t need to build a server socket to
listen for any request from web client, parse the request and generate
response. All these important and complex tasks are done by container
and all we need to focus is on business logic for the applications.
2. Lifecycle and Resource Management: Servlet Container takes care
of managing the life cycle of servlet. From the loading of servlets into
memory, initializing servlets, invoking servlet methods and to destroy
them. Container also provides utility like JNDI for resource pooling and
management.
3. Multithreading Support: Container creates new thread for every request
to the servlet and provide them request and response objects to
process. So servlets are not initialized for each request and saves time
and memory.
4. JSP Support: JSPs doesn’t look like normal java classes but every JSP
in the application is compiled by container and converted to Servlet and
then container manages them like other servlets.
5. Miscellaneous Task: Servlet container manages the resource
pool, perform memory optimizations, execute garbage collector,
provides security configurations, support for multiple applications, hot
deployment and several other tasks behind the scene that makes a
developer life easier.
36. RequestDispatcher interface is used to forward the request to another resource that
can be HTML, JSP or another servlet in same application. We can also use this to
include the content of another resource to the response. This interface is used for
inter-servlet communication in the same context.
37. There are two methods defined in this interface:
1. void forward(ServletRequest request, ServletResponse response) –
forwards the request from a servlet to another resource (servlet, JSP
file, or HTML file) on the server.
2. void include(ServletRequest request, ServletResponse response) –
includes the content of a resource (servlet, JSP page, HTML file) in the
response.
3. We can get RequestDispatcher in a servlet using ServletContext
getRequestDispatcher(String path) method. The path must begin with a
/ and is interpreted as relative to the current context root.
45. We can create deadlock in servlet by making a loop of method invocation, just call
doPost() method from doGet() method and doGet() method to doPost() method to
create deadlock situation in servlet.
46. Read more about deadlock in multithreading at Java Deadlock Example.
52. When servlet container receives client request, it invokes the service() method
which in turn invokes the doGet(), doPost() methods based on the HTTP method of
request. I don’t see any use case where we would like to override service() method.
The whole purpose of service() method is to forward to request to corresponding
HTTP method implementations. If we have to do some pre-processing of request,
we can always use servlet filters and listeners.
54. We can define a constructor for servlet but I don’t think its of any use because
we won’t be having access to the ServletConfig object until unless servlet is
initialized by container. Ideally if we have to initialize any resource for servlet, we
should override init() method where we can access servlet init parameters using
ServletConfig object.
55. What is difference between GenericServlet and
HttpServlet?