InterviewSolution
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»
|
|||||||||||||||||||||||||||||||||||||||||
| 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.
|
|
| 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 <SERVLET> TAG is also RETURNED. The <SERVLET> tag is then substituted by the corresponding servlet code. |
|
| 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:
|
|
| 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: |
|
| 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.) |
|