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 javax.servlet package?

Answer»

The core of the Servlet API is the javax.servlet package. It includes the basic Servlet interface, which all SERVLETS must implement in one form or another, and an abstract GenericServlet class for developing basic servlets.

This package comprises of the following:

  • Classes for communicating with the host server and client (ServletRequest and ServletResponse)
  • Communicating with the client (ServletInputStream and ServletOutputStream).

In SITUATIONS where the underlying protocol is unknown, servlets should confine themselves to the classes within this package. 

Conclusion:

Unlike CGI and FastCGI, which use many processes to handle separate programs and separate requests, servlets are all handled by separate threads within the webserver process. Thus, the servlets are efficient and scalable. As servlets run within the web server, they can interact very closely with the server to do things that are not possible with CGI scripts.

An advantage of servlets is that they are portable: both ACROSS operating systems like with Java and also across web servers.

All of the major web servers support servlets. 

References and Resources:

  • Java Servlet PROGRAMMING, by Jason Hunter, Published by O'Reilly Media, Inc.
  • Java Servlet & JSP Cookbook, by Bruce W. Perry
  • Java Interview
  • JSP Interview
  • JSP vs Servlet
2.

What do you mean by Servlet Manipulation?

Answer»

When ONE servlet accesses the LOADED SERVLETS on its server, it is called Servlet Manipulation. It also optionally performs some task on one or more of them. A servlet gets information about other servlets through the SERVLETCONTEXT object. We use getServlet() to get a particular servlet:

public Servlet ServletContext.getServlet(String name) throws ServletException
3.

What are the reasons we use inter-servlet communication?

Answer»

There are three major REASONS to use the inter SERVLET COMMUNICATION:

  • DIRECT servlet manipulation
  • Servlet reuse
  • Servlet collaboration
4.

What are the three methods of inter-servlet communication?

Answer»

The three methods of inter servlet communication are:

  • Servlet manipulation: In Servlet manipulation, one servlet directly invokes the methods of another. These servlets can GET references to other servlets using getServletNames() and getServlet(String NAME).
  • Servlet reuse: In Servlet reuse, one servlet uses another’s ABILITIES for its own purposes. In some cases, this requires forcing a servlet load using a manual HTTP request.
  • Servlet collaboration: In Servlet collaboration, the cooperating servlets share information. Servlets can share information using the system properties list, using a SHARED object, or using inheritance.
5.

Explain Request parameters associated with servlets.

Answer»

There can be any VARIETY of REQUEST parameters related to the SERVLET with every access to it. These parameters are usually [name-value] pairs that give the servlet any further information that it desires so as to handle the request.

An HTTP servlet gets its request parameters as a part of its query string or as encoded post data. A servlet used as a server-side includes its parameters equipped with PARAM tags.

Fortunately, although a servlet will receive parameters in an exceeding variety of various ways, every servlet RETRIEVES its parameters the same way, by using getParameter() and getParameterValues() :

public String ServletRequest.getParameter(String name)public String[] ServletRequest.getParameterValues(String name)
6.

How does Servlet collaboration take place?

Answer»

Servlets RUNNING TOGETHER in the same server have many ways to communicate with one another. There are two main STYLES of servlet collaboration:

  • SHARING information: Sharing information involves two or more servlets sharing the state or even RESOURCES. A special case of sharing information is Session tracking.
  • Sharing control: Sharing control involves two or more servlets sharing control of the request. For example, one servlet could receive the request but let another servlet handle some or all of the request-handling responsibilities.
7.

How does Background Processing take place in servlets?

Answer»

SERVLETS can do more than just persist between the accesses. They can ALSO execute between accesses. A thread that has been started by a servlet can CONTINUE to execute even after the response has been sent. This ability proves most useful for the tasks that are long-running, and WHOSE incremental results should be MADE available to multiple clients. A background thread that has been started in init() performs continuous work. It also performs request-handling threads displaying the current status with doGet(). 

8.

Explain the Single-Thread Model in servlets.

Answer»

It is STANDARD to have a single servlet instance for each registered name of the servlet. However, instead of this, it is also possible for a servlet to choose to have a pool of instances created for each of its names that all share the task of handling requests. These servlets indicate this action by implementing the javax.servlet.SingleThreadModel interface.

According to the Servlet API documentation, a SERVER LOADING the SingleThreadModel servlet should guarantee, “that no two threads will execute CONCURRENTLY the service method of that servlet.” Each thread uses a free servlet instance from the pool in order to achieve this. Therefore, any servlet using the SingleThreadModel isn’t needed to synchronize USAGE to its instance variables and is considered thread-safe.

9.

How can a servlet get information about the client machine?

Answer»

A servlet can USE getRemoteAddr() and getRemoteHost() to retrieve the IP ADDRESS and hostname of the CLIENT machine, respectively:

public String ServletRequest.getRemoteAddr()public String ServletRequest.getRemoteHost()

Both values are RETURNED as String objects. 

10.

How can a servlet get the name of the server and the port number for a particular request?

Answer»

A servlet can get the name of the server and the port number for a particular request with getServerName() and getServerPort(), respectively:

public STRING ServletRequest.getServerName()public int ServletRequest.getServerPort()

These METHODS are attributes of ServletRequest because the values can change for different requests if the server has more than one name (a TECHNIQUE called virtual hosting).

The getServerInfo() and getAttribute() methods of ServletContext supply INFORMATION about the server software and its attributes:

public String ServletContext.getServerInfo()public OBJECT ServletContext.getAttribute(String name)
11.

What are the methods that a servlet can use to get information about the server?

Answer»

A servlet can be USED to learn about its SERVER USING 4 different methods. Out of these, two methods are called using the ServletRequest OBJECT. These are passed to the servlet. The other two are called from the ServletContext object. In these, the servlet is executing. 

12.

What do you mean by Servlet Reloading?

Answer»

Servlet reloading may appear to be a simple FEATURE, but it’s actually quite a trick—and requires quite a hack. The objects in CLASSLOADER are developed to load a class just once. To solve this limitation and to load servlets multiple times, servers use custom class loaders. These custom class loaders load servlets from the default servlets directory.

When a SERVER dispatches a request to a servlet, it FIRST checks if the servlet’s class file has changed on disk. If the change appears, then the server abandons the class that the LOADER used to load the old version and then creates a new instance of the custom class loader to load the new version. Old servlet versions can stay in memory indefinitely, but the old versions are not used to handle any more requests.

13.

What is the life cycle contract that a servlet engine must conform to?

Answer»

The life CYCLE contract that a SERVLET engine must CONFORM to is as follows:

  • Create the servlet and INITIALIZE it.
  • Manage none or more calls for service from clients.
  • Destroy the servlet and then the GARBAGE collects it.
14.

Explain the Servlet Life Cycle.

Answer»

One of the most striking features of servlets is the Servlet Life Cycle. This is a powerful mixture of the life cycles used in CGI PROGRAMMING and lower-level NSAPI and ISAPI programming.

The CGI has CERTAIN resource and performance problems. In low-level server API programming, there are some security concerns as well. These are addressed by the servlet engines by the servlet life cycle. A servlet engine might execute all of its servlets in a single Java virtual machine (JVM). Servlets can efficiently share data with each other as they share the same JVM. Still, they are prevented from accessing each other’s private data by the Java language. Additionally, servlets can be permitted to remain between requests as object instances. Thus they take up lesser memory than the complete PROCESSES.

15.

What are the advantages of Servlet chains?

Answer»

Servlet chains have the following advantages:

  • Servlet chains can be UNDONE easily. This helps in quickly reversing the change.
  • Servlet chains dynamically handle CONTENT that is created. Because of this, ONE can TRUST that all our restrictions are maintained, that the special tags are replaced, and even in the output of a servlet, all the dynamically converted PostScript images are properly displayed.
  • Servlet chains cache the content for later, so it does not execute the script every time got added.
16.

What are the uses of Servlet chaining?

Answer»

Given below are some of the use cases of Servlet chaining:

  • Change how a group of PAGES, a single page, or a type of CONTENT appears quickly

One can TALK to those who don’t understand a particular language by dynamically translating the text from the pages to the language that can be read by the client. One can keep away certain words that one doesn’t want others to read.

  • Display in special formats a KERNEL of content

For instance, one can add custom tags within a page, and then a servlet can replace these with HTML content.

  • Support for the esoteric data types

For instance, one can PROVIDE a filter that converts nonstandard image types to GIF or JPEG for the unsupported image types.

17.

What do you mean by ‘filtering’ in servlets?

Answer»

There are usually 2 ways during which ONE will trigger a series of servlets for an associate incoming request. In the first MANNER, it is such that the server that bound URLs ought to be handled with the associated specified chain. the other manner is that one will inform the server to redirect all the OUTPUT of a selected content through a selected SERVLET before it's returned to the client. This effectively creates a series on the FLY. once a servlet transforms one sort of content into another, this method is named filtering.

18.

What do you mean by Servlet chaining?

Answer»

Servlet Chaining is a way where the output of one servlet is piped to the input of another servlet, and the output of that servlet can be piped to the input of yet another servlet and so on. Each servlet in the pipeline can EITHER change or EXTEND the incoming request. The response is RETURNED to the browser from the last servlet within the servlet chain. In the middle, the output out of each servlet is passed as the input to the NEXT servlet, so every servlet within the chain has an option to either change or extend the content. The figure below represents this. Servlets can help in creating content via servlet chaining.