Servlet Interview Questions and Answers

1.When to use doGet() and when doPost()?
Always prefer to use GET (As because GET is faster than POST), except mentioned in the following reason:
If data is sensitive
Data is greater than 1024 characters
If your application don't need bookmarks.
17.How do I support both GET and POST from the same Servlet?
The easy way is, just support POST, then have your doGet method call your doPost method:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
18.Should I override the service() method?
We never override the service method, since the HTTP Servlets have already taken care of it . The default service function invokes the doXXX() method corresponding to the method of the HTTP request.For example, if the HTTP request method is GET, doGet() method is called by default. A servlet should override the doXXX() method for the HTTP methods that servlet supports. Because HTTP service method check the request method and calls the appropriate handler method, it is not necessary to override the service method itself. Only override the appropriate doXXX() method.
19.How the typical servlet code look like ?

20.What is a servlet context object?
A servlet context object contains the information about the Web application of which the servlet is a part. It also provides access to the resources common to all the servlets in the application. Each Web application in a container has a single servlet context associated with it.
21.What are the differences between the ServletConfig interface and the ServletContext interface?
ServletConfig ServletContext
The ServletConfig interface is implemented by the servlet container in order to pass configuration information to a servlet. The server passes an object that implements the ServletConfig interface to the servlet's init() method. A ServletContext defines a set of methods that a servlet uses to communicate with its servlet container.
There is one ServletConfig parameter per servlet. There is one ServletContext for the entire webapp and all the servlets in a webapp share it. The param-value pairs for ServletConfig object are specified in the within the tags in the web.xml file The param-value pairs for ServletContext object are specified in the tags in the web.xml file.
22.What's the difference between forward() and sendRedirect() methods?
forward()
sendRedirect()

A forward is performed internally by the servlet. A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original.


The browser is completely unaware that it has taken place, so its original URL remains intact. The browser, in this case, is doing the work and knows that it's making a new request.

Any browser reload of the resulting page will simple repeat the original request, with the original URL A browser reloads of the second URL ,will not repeat the original request, but will rather fetch the second URL.

Both resources must be part of the same context (Some containers make provisions for cross-context communication but this tends not to be very portable) This method can be used to redirect users to resources that are not part of the current context, or even in the same domain.

Since both resources are part of same context, the original request context is retained Because this involves a new request, the previous request scope objects, with all of its parameters and attributes are no longer available after a redirect.

(Variables will need to be passed by via the session object). Forward is marginally faster than redirect. redirect is marginally slower than a forward, since it requires two browser requests, not one.

23.What is the difference between the include() and forward() methods?
include()
forward()

The RequestDispatcher include() method inserts the the contents of the specified resource directly in the flow of the servlet response, as if it were part of the calling servlet.

The RequestDispatcher forward() method is used to show a different resource in place of the servlet that was originally called.

If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored. The forwarded resource may be another servlet, JSP or static HTML document, but the response is issued under the same URL that was originally requested. In other words, it is not the same as a redirection.

The include() method is often used to include common "boilerplate" text or template markup that may be included by many servlets. The forward() method is often used where a servlet is taking a controller role; processing some input and deciding the outcome by returning a particular response page.


1.What is the Servlet?

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request- response programming model.


2.What are the new features added to Servlet 2.5?
Following are the changes introduced in Servlet 2.5:
A new dependency on J2SE 5.0
Support for annotations
Loading the class
Several web.xml conveniences
A handful of removed restrictions
Some edge case clarifications
3.What are the uses of Servlet?
Typical uses for HTTP Servlets include:
Processing and/or storing data submitted by an HTML form.

Providing dynamic content, e.g. returning the results of a database query to the client. A Servlet can handle multiple request concurrently and be used to develop high performance system Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.


4.What are the advantages of Servlet over CGI?
Servlets have several advantages over CGI:

A Servlet does not run in a separate process. This removes the overhead of creating a new process for each request. A Servlet stays in memory between requests. A CGI program (and probably also an extensive runtime system or interpreter) needs to be loaded and started for each CGI request.


There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Several web.xml conveniences
A handful of removed restrictions
Some edge case clarifications
5.What are the phases of the servlet life cycle?
The life cycle of a servlet consists of the following phases:

Servlet class loading : For each servlet defined in the deployment descriptor of the Web application, the servlet container locates and loads a class of the type of the servlet. This can happen when the servlet engine itself is started, or later when a client request is actually delegated to the servlet.

Servlet instantiation : After loading, it instantiates one or more object instances of the servlet class to service the client requests.
Initialization (call the init method) :

After instantiation, the container initializes a servlet before it is ready to handle client requests. The container initializes the servlet by invoking its init() method, passing an object implementing the ServletConfig interface. In the init() method, the servlet can read configuration parameters from the deployment descriptor or perform any other one-time activities, so the init() method is invoked once and only once by the servlet container.

Request handling (call the service method) :

After the servlet is initialized, the container may keep it ready for handling client requests. When client requests arrive, they are delegated to the servlet through the service() method, passing the request and response objects as parameters. In the case of HTTP requests, the request and response objects are implementations of HttpServletRequest and HttpServletResponse respectively. In the HttpServlet class, the service() method invokes a different handler method for each type of HTTP request, doGet() method for GET requests, doPost() method for POST requests, and so on.

Removal from service (call the destroy method) :

A servlet container may decide to remove a servlet from service for various reasons, such as to conserve memory resources. To do this, the servlet container calls the destroy() method on the servlet. Once the destroy() method has been called, the servlet may not service any more client requests. Now the servlet instance is eligible for garbage collection The life cycle of a servlet is controlled by the container in which the servlet has been deployed.

10 comments:

  1. Thanks for the article, excellent stuff.
    I have seen interesting Interview Questions and Answers here.

    ReplyDelete
  2. Thanks for this type of good content..

    ReplyDelete
  3. I recommends you to try same type of good content Java interview question and answer

    ReplyDelete
    Replies
    1. It is very helpful blog that provide good interview question and answer.
      firstly i says thanks to blog owner and mr. rahul yadav for provide me a useful link.

      Delete
  4. important servlet interview questions with answer
    http://www.itsoftpoint.com/?page_id=2425

    ReplyDelete
  5. HI, Friends this very usefull java servlet interview questions
    for excellent knowledge on java servlet visit http://www.itsoftpoint.com/?page_id=2425

    ReplyDelete
  6. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
    Digital Marketing Training in Chennai

    Digital Marketing Course in Chennai

    ReplyDelete