Evalueserve Java Questions: Servlets

Download PDF of This Page (Size: 131K)

Examrace Placement Series prepares you for the toughest placement exams to top companies.

  1. What is the servlet? ANSWER: Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company's order database. Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

  2. Whats the advantages using servlets than using CGI? ANSWER: Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: They are developed with the Java Servlet API, a standard Java extension.

  3. What are the uses of Servlets? ANSWER: A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.

  4. Which pakage provides interfaces and classes for writing servlets? ANSWER: Javax

  5. Whats the Servlet Interfcae? 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. Servlets → Generic Servlet → HttpServlet → MyServlet. The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet.

  6. When a servlet accepts a call from a client, it receives two objects-What are they? ANSWER: ServeltRequest: Which encapsulates the communication from the client to the server. ServletResponse: Whcih encapsulates the communication from the servlet back to the client. ServletRequest and ServletResponse are interfaces defined by the javax. Servlet package.

  7. What information that the ServletRequest interface allows the servlet access to? ANSWER: Information such as the names of the parameters passed in 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. Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods.

  8. What information that the ServletResponse interface gives the servlet methods for replying to the client? ANSWER: It Allows the servlet to set the content length and MIME type of the reply. Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data.

  9. What is the servlet Lifecycle? ANSWER: Each servlet has the same life cycle: A server loads and initializes the servlet (init () ) The servlet handles zero or more client requests (service () ) The server removes the servlet (destroy () ) (some servers do this step only when they shut down)

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