Top 30 Java Servlets Interview Questions You Must Prepare 04.Dec.2022

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.

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.

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.

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.

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

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

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.

1.GenericServlet is an abstract class which implements the Servlet interface and the ServletConfig interface.
2.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.
3.Since this class is not specific to any protocol, it is known as generic servlet.

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

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.

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.

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.

For maintaining session information Servlet Container uses:
. Cookies
. URL rewriting
. HTTPS protocol information

The standard HTTP protocols ways of refreshing the page, which is normally supported by all browsers.
<META HTTP-EQUIV="Refresh" CONTENT="5; URL=/servlet/MyServlet/">
This will refresh the page in the browser automatically and loads the new data every 5 seconds.

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.

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

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.

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.

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.

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.

Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the <servlet> 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 <context-param> directly inside the <web-app> tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);

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.

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.

The three major reasons to use inter servlet communication are:
a) Direct servlet manipulation - allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)
b) Servlet reuse - allows the servlet to reuse the public methods of another servlet.
c) Servlet collaboration - requires to communicate with each other by sharing specific information (through method invocation).

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.

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

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.

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 <load-on-startup> 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.

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.

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.