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.

How does a servlet examine all its init parameters?

Answer»

We can MAKE use of getInitParameterNames() function to examine all its init parameters.

public Enumeration ServletConfig.getInitParameterNames()

This returns the names of the servlet's INITIALIZATION parameters as an Enumeration of STRING objects, or an EMPTY Enumeration if the servlet has no initialization parameters. This is OFTEN used for debugging.

2.

How does a servlet get access to its init parameters?

Answer»

The getInitParameter() method is used by the servlet in order to GET access to its init parameters:

public String ServletConfig.getInitParameter(String name)

The above method returns the value of the NAMED init parameter or if the named init parameter does not exist it will RETURN null. The value returned is ALWAYS a SINGLE string. The servlet then interprets the value.

3.

Compare CGI Environment Variables and the Corresponding Servlet Methods.

Answer»
CGI ENVIRONMENT VariableHTTP SERVLET Method
SERVER_NAMEreq.getServerName()
SERVER_SOFTWAREgetServletContext().getServerInfo()
SERVER_PROTOCOLreq.getProtocol()
SERVER_PORTreq.getServerPort()
REQUEST_METHODreq.getMethod()
PATH_INFOreq.getPathInfo()
PATH_TRANSLATEDreq.getPathTranslated()
SCRIPT_NAMEreq.getServletPath()
DOCUMENT_ROOTreq.getRealPath("/")
QUERY_STRINGreq.getQueryString()
REMOTE_HOSTreq.getRemoteHost()
REMOTE_ADDRreq.getRemoteAddr()
AUTH_TYPEreq.getAuthType()
REMOTE_USERreq.getRemoteUser()
CONTENT_TYPEreq.getContentType()
CONTENT_LENGTHreq.getContentLength()
HTTP_ACCEPTreq.getHeader("ACCEPT")
HTTP_USER_AGENTreq.getHeader("User-Agent")
HTTP_REFERERreq.getHeader("REFERER")
4.

How is retrieving information different in Servlets as compared to CGI?

Answer»

Servlets have a variety of WAYS to realize access to information. For the bulk of it, every method returns a specific result. Compared with CGI programs its information by making use of passed environment variables, ONE can see multiple advantages by using the servlet approach.

  • Stronger type checking:
    Stronger type checking MEANS that there is more support in the compiler for finding errors in syntax and types. A CGI program utilizes one function to get its environment variables, and several errors are not caught at compile-time and they get only know at runtime cannot be caught until some runtime issue got caused.
  • Delayed calculation:
    The value for every environment variable has to be precalculated and passed when a server starts a CGI program, even if the program uses it or not. In contrast, servlets launched by servers can enhance the performance on the fly by delaying calculation and do calculations when that PIECE of code is actually used.
  • Interactives with the server:
    A CGI program is free from its server, once the execution BEGINS. Then, the single communication path that the program uses is its standard output. However, a servlet can work with the server. A servlet works in 2 ways: either in the server or as a connected sidecar process outside the server.
5.

Define ‘init’ and ‘destroy’ methods in servlets.

Answer»

SERVLETS ​​Init Method is used to INITIALISE a SERVLET.

After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet. To customize this process to allow the servlet to read persistent configuration data, initialize RESOURCES, and PERFORM any other one-time activities, you override the init method of the Servlet interface.

Example: 

public class CatalogServlet extends HttpServlet {private ArticleDBAO articleDB;public void init() throws ServletException {articleDB = (ArticleDBAO)getServletContext().getAttribute("articleDB");if (articleDB == null) throw newUnavailableException("Database not loaded");}}

When a servlet container determines that a servlet should be removed from service (for example, when a container wants to reclaim memory resources or when it is being shut down), the container calls the destroy method of the Servlet interface.

The following destroy method releases the database object created in the init method.

public void destroy() {bookDB = null;}
6.

Explain the server-side include expansion.

Answer»

Server-side inclusion (SSI) is a feature of a server in which a placeholder &LT;SERVLET&GT; TAG is also RETURNED. The <SERVLET> tag is then substituted by the corresponding servlet code.
The server just parses the pages that are specially tagged, and it doesn’t parse and analyses each page it returns. The Java Web Server parses solely pages with a .shtml extension by default. With the SERVLET tag, in contrast to the APPLET tag, the client web browser doesn’t see anything between SERVLET and /SERVLET UNLESS SSI is not supported by the server.

7.

What do you mean by server-side include (SSI) functionality in Servlets?

Answer»

Servlets can be ADDED in HTML pages with the SERVER-side include (SSI) functionality. A page can be preprocessed by the server to add the output from servlets at some points within the page, in the servers that support servlets.

<SERVLET CODE=ServletName CODEBASE=http://server:port/dir initParam1=initValue1 initParam2=initValue2><PARAM NAME=param1 VALUE=val1><PARAM NAME=param2 VALUE=val2> Text appearing here indicates that the web server which provides this page does not support the SERVLET tag.</SERVLET>
8.

Explain the Servlet API.

Answer»

A servlet does not have a main() method, unlike a regular Java program, and just like an applet. It has some methods of a servlet that are called upon by the server for the purpose of handling requests. It invokes the servlet’s service() method, EVERY time the server sends a request to a servlet.

To handle requests that are APPROPRIATE for the servlet, a typical servlet must override its service() method. The service() method allows 2 parameters: these are the request OBJECT and the response object. The request object is used to inform the servlet about the request, whereas the response object is used to then give a response.

As opposed to this, an HTTP servlet typically does not override the service() method. However, it actually overrides the doGet() to handle the GET requests and the doPost() to handle POST requests. Depending on the TYPE of requests it needs to handle, an HTTP servlet can override either or both of these methods. 

9.

What are some of the advantages of Servlets?

Answer»

Servlets provide a NUMBER of advantages over the other approaches. These include power, integration, efficiency, safety, portability, endurance, elegance, extensibility, and also flexibility. Here are the advantages of servlets:

  • A SERVLET is convenient in modifying regular HTML
  • We can write the servlet code into the JSP
  • Servlets includes the feature of multithreading of java
  • We can make use of exception handling
  • Servlets have a separate layer of business LOGIC in the application
  • Easy for developers to show and process the information.
  • Servlets provide a convenient way to MODIFY HTML pages.
  • Servlets have a separate layer of business logic in the application.
  • All the advantages of Java-like multi-threading, exception handling, etc. are there in Servlets
10.

How do you write a servlet that is part of a web application?

Answer»

To write a servlet that is part of a web application:
CREATE a Java CLASS that extends javax.servlet.http.HttpServlet. 
Import the classes from servlet.jar (or servlet-api.jar). 
These will be needed to compile the servlet.

11.

What is a Servlet?

Answer»

A servlet is a small Java program that runs within a Web SERVER. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol. Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and CRASH protection. Servlets are often used to provide rich INTERACTION FUNCTIONALITY within the browser for users (clicking link, form submission, etc.)