MENU
Home » 2016 » April » 1 » Servlets Interview Questions
4:47 PM
Servlets Interview Questions





#jsp interview questions

#Servlets Interview Questions

Previous Page

Dear readers, these Servlets Interview Questions have been designed especially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Servlets Programming. As per my experience, good interviewers hardly planned to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer:

What are Servlets?

Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

Servlets offer several advantages in comparison with the CGI.

Performance is significantly better.

Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.

Servlets are platform-independent because they are written in Java.

Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.

The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.

What are the major tasks of servlets?

Servlets perform the following major tasks:

Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.

Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.

Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.

Send the explicit data (i.e. the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.

Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g. HTML), setting cookies and caching parameters, and other such tasks.

Explain servlet life cycle.

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.

The servlet is initialized by calling the init () method.

Finally, servlet is garbage collected by the garbage collector of the JVM.

The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.

A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

The destroy() method is called only once at the end of the life cycle of a servlet.

The init() method simply creates or loads some data that will be used throughout the life of the servlet.



Views: 344 | Added by: mega_tyfuk-1982 | Tags: questions, Servlets, Interview | Rating: 0.0/0
Total comments: 0
avatar