Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What Is The Difference Between Servletcontext And Servletconfig?

Answer»

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.

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.

2.

What's The Servlet Interface?

Answer»

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.

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.

3.

What Is Server Side Push?

Answer»

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.

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.

4.

What Is Client Side Refresh?

Answer»

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.

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.

5.

Why Should We Go For Inter Servlet Communication?

Answer»

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 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).

6.

What Are The Differences Between A Session And A Cookie?

Answer»

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.

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.

7.

What Is Http Tunneling?

Answer»

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.

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.

8.

What Are The Different Ways For Getting A Servlet Context?

Answer»

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.

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.

9.

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

Answer»

Servlet init parameters are for a single servlet only. No body out SIDE that servlet can access that. It is declared inside the <servlet&GT; 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”);

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”);

10.

How Do You Communicate Between The Servlets?

Answer»

 We can COMMUNICATE between servlets by USING RequestDespatcher INTERFACE and servlet chaining.

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

11.

How Will You Communicate From An Applet To Servlet?

Answer»

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.

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.

12.

What Is Servlet Chaining?

Answer»

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.

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.

13.

How Do Servlets Handle Multiple Simultaneous Requests?

Answer»

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.

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.

14.

What Is Pre Initialization Of A Servlet?

Answer»

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.

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.

15.

How Http Servlet Handles Client Requests?

Answer»

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.

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.

16.

What Are The Uses Of Servletresponse Interface?

Answer»

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.

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.

17.

What Are The Uses Of Servletrequest?

Answer»

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 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.

18.

What Is The Web Container?

Answer»

A SERVLET and JSP CONTAINERS are collectively referred to as Web containers.

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

19.

What Is The Procedure For Initializing A Servlet?

Answer»

- To initialize a SERVLET INIT() is used. 
- init() initializes a JAVA PROGRAM.
- A constructor can also be used to initialize a servlet.

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

20.

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

Answer»

For maintaining SESSION information SERVLET CONTAINER uses:
. Cookies
. URL REWRITING
. HTTPS protocol information

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

21.

What Is Lazy Loading?

Answer»

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.

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.

22.

How Can The Session In Servlet Be Destroyed?

Answer»

There are two ways to destroy a session:
1. PROGRAMATICALLY : By USING session.invalidate() method. It makes the container ABANDON the session on which the method is called.
2. When the server shuts down.

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

23.

What Is Genericservlet Class?

Answer»

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.

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.

24.

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

Answer»

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.

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.

25.

Why Is Httpservlet Declared Abstract?

Answer»

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.

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.

26.

How Would You Create Deadlock On Your Servlet?

Answer»

CALLING a DOPOST() method INSIDE doGet() and doGet()method inside doPost() wouleate a DEADLOCK for a servlet.

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

27.

What Is A War File?

Answer»

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.

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.

28.

What Is A Servlet Filter?

Answer»

SERVLET filters are pluggable WEB components that ALLOW us to IMPLEMENT pre-processing and post-processing LOGIC in our Web applications.

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

29.

What Is Servlet Context?

Answer»

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.

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.