300+ TOP Java Servlets Interview Questions and Answers

Java Servlets Interview Questions

1. What are the Differences between a Session and a Cookie?

Session is stored in server but cookie stored in client. Session should work regardless of the settings on the client browser. There is no limit on the amount of data that can be stored on session. But it is limited in cookie. Session can store objects and cookies can store only strings. Cookies are faster than session.

2. What are The Different Ways For Getting A Servlet Context?

We will get ServletContext by calling getServletConfig ().getServletContext (). This is because a ServletConfig always hold a reference to ServletContext. By calling this.getServletContext () also we will get a ServletContext object.

3. Why Is Http servlet Declared Abstract?

  1. The default implementations of the main service methods can not do anything and need to be overridden. This calls of the HttpServlet class to be declared as abstract.
  2. With its use the developers do not need to implement all the service methods.

4. What Is Server Side Push?

Server Side push is useful when data needs to change regularly on the clients application or browser, without intervention from client. The mechanism used is, when client first connects to Server, then Server keeps the TCP/IP connection open.

5. What Is A Servlet Filter?

Servlet filters are pluggable Web components that allow us to implement pre-processing and post-processing logic in our Web applications.

6. What Is The Procedure For Initializing A Servlet?

  • To initialize a servlet init() is used.
  • init() initializes a java program.
  • A constructor can also be used to initialize a servlet.

7. What Is Lazy Loading?

The servlets are not initialized by the container from the start. It happens when the servlet is requested for the first time. This is called lazy loading.

8. What Is Genericservlet Class?

  • GenericServlet is an abstract class which implements the Servlet interface and the ServletConfig interface.
  • Other than the methods included in above two interfaces, it also provides simple versions of the lifecycle methods init and destroy, and implements the log method declared in the ServletContext interface.
  • Since this class is not specific to any protocol, it is known as generic servlet.

9. How Do You Communicate Between The Servlets?

We can communicate between servlets by using RequestDespatcher interface and servlet chaining.

10. How Do Servlets Handle Multiple Simultaneous Requests?

When a request comes in, the web server will start a new thread and the request is assigned to a thread, which calls a service method of the servlet.

11. What Are The Uses Of Servlet request?

The ServletRequest gives information such as the names of the parameters passed by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.

12. What Is The Difference Between Servletcontext And Servletconfig?

The ServletConfig gives the information about the servlet initialization parameters. The servlet engine implements the ServletConfig interface in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet’s init() method.
The ServletContext gives information about the container. The ServletContext interface provides information to servlets regarding the environment in which they are running. It also provides standard way for servlets to write events to a log file.

13. What Are The Mechanisms Used By A Servlet Container For Maintaining Session Information?

For maintaining session information Servlet Container uses:

  • Cookies
  • URL rewriting
  • HTTPS protocol information

14. What Is Client Side Refresh?

The standard HTTP protocols ways of refreshing the page, which is normally supported by all browsers.

This will refresh the page in the browser automatically and loads the new data every 5 seconds.

15. How Can The Session In Servlet Be Destroyed?

There are two ways to destroy a session:
@Programatically : By using session.invalidate() method. It makes the container abandon the session on which the method is called.
@When the server shuts down.

16. What Is The Web Container?

A Servlet and  JSP containers are collectively referred to as Web containers.

17. What Is Http Tunneling?

HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intranet is blocked by a firewall and the network is exposed to the outer world only through a specific Web server port, that listens for only HTTP requests. To use any other protocol, that by passes the firewall, the protocol is embedded in HTTP and send as HttpRequest.

18. How Http Servlet Handles Client Requests?

An HTTP Servlet handles client requests through its service method. The service method supports standard HTTP client requests by dispatching each request to a method designed to handle that request.

19. How Will You Communicate From An Applet To Servlet?

There are three ways to communicate from an applet to servlet and they are: HTTP Communication (Text-based and object-based) , Socket Communication and RMI Communication.

20. What’s The Servlet Interface?

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.

21. What Is The Difference Between Context Init Parameter And Servlet Init Parameter?

Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the tag inside Deployment Descriptor, where as context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag directly inside the tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);

22. Why Is A Constructor Needed In A Servlet Even If We Use The Init Method?

  1. Although the init method of the servlet initializes it, a constructor instantiates it.
  2. A developer might never explicitly call the servlet’s constructor but a container uses it to create an instance of the servlet.

23. What Are The Uses Of Servletresponse Interface?

ServletResponse allows the servlet to set the content length and MIME type of that response. It provides an output stream, ServletOutputStream and a Writer through which the servlet can send data.

24. Why Should We Go For Inter Servlet Communication?

The three major reasons to use inter servlet communication are:

  • Direct servlet manipulation – allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)
  • Servlet reuse – allows the servlet to reuse the public methods of another servlet.
  • Servlet collaboration – requires to communicate with each other by sharing specific information (through method invocation).

25. What Is Servlet Context?

The Servlet context is an object that contains a information about the Web application and container. Using the context, a Servlet can log events, obtain URL references to resources, and set and store attributes that other Servlet in the context can use.

26. How Would You Create Deadlock On Your Servlet?

Calling a doPost() method inside doGet() and doGet()method inside doPost() wouleate a deadlock for a servlet.

27. What Is Servlet Chaining?

Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request. In servlet chaining, one servlet’s output is the input of next servlet. This process continues until the last servlet is reached. Its output is then sent back to the client. We are achieving Servlet Chaining with the help of RequestDispatcher.

28. What Is Pre Initialization Of A Servlet?

A container doesn’t initialize the servlets when it starts up. It initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or pre initializing a servlet.

29. Can We Use The Constructor, Instead Of Init(), To Initialize Servlet?

Yes. But you will not get the Servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

30. What Is A War File?

WAR stands for Web Archive. It is a compressed version of your web application. You can use this WAR file to deploy your web application.

Java Servlets Questions with Answers Pdf Download