• +91 9723535972
  • info@interviewmaterial.com

Servlet Interview Questions and Answers

Servlet Interview Questions and Answers

Question - 91 : - What are the uses of Servlet chaining?

Answer - 91 : -

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.

Question - 92 : - What are the advantages of Servlet chains?

Answer - 92 : -

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.

Question - 93 : - Explain the Servlet Life Cycle.

Answer - 93 : -

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.

Question - 94 : - What is the life cycle contract that a servlet engine must conform to?

Answer - 94 : -

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.

Question - 95 : - What do you mean by Servlet Reloading?

Answer - 95 : -

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.

Question - 96 : - What are the methods that a servlet can use to get information about the server?

Answer - 96 : -

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. 

Question - 97 : - How can a servlet get information about the client machine?

Answer - 97 : -

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. 

Question - 98 : - How does Servlet collaboration take place?

Answer - 98 : -

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.

Question - 99 : - Explain Request parameters associated with servlets.

Answer - 99 : -

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)

Question - 100 : - What are the three methods of inter-servlet communication?

Answer - 100 : -

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.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners