Servlet Interview Questions and Answers

    1 Votes

35. How servlet works with buffering?

Assuming buffer size is set to 150 kb only if the buffer is filled with 150 bytes of data the response will be sent to client. So buffering allows the servlet to control the response till the buffer gets filled up.

36. What is servlet chaining?

Servlet chaining is a technique in which two or more servlets orchestrate in servicing a single request. In servlet chaining, one servlet’s response is piped to next servlet’s input. This process continues until the last servlet is reached. Its output is sent back to the client. The same request and response objectare available across all the servlets in the process.

37. Explain about two ways of servlet chaining?

Two ways of servlet chaining are:

  • Include: This refers to the process of including the response of one or more servlets with the response of the servlet being requested and the collated response sent to the client by the servlet being requested.
  • Forward: The servlet being requestesd by the client forwards the request to one or more servlet and finally the response is sent to the client by last servlet invoked in the Chain.

38. How do we chain servlets?

  • Create the RequestDispatcher for the resources to be chained.
  • Set attributes to request if needed.
  • Call the include or forward method on the RequestDispatcher object.

The include/forward method can be performed only in the service methods(get/post) of a servlet and in turn can be handled in the service method of the included/forwarded servlet. Example: Include from a get method in first servlet can be handled only by get method in the included servlet. Same applies to post method also.

39. What is a RequestDispatcher object?

RequestDispatcher is an object which accepts the request from the client and redirects them to any resource(Servlets, JSP, HTML etc). The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or identified by a particular name.

40. How do we get RequestDispatcher object?

RequestDispatcher dispatcher = request.getRequestDispatcher(“FooterServlet”);

Where request is the HTTPServletRequest object and FooterServlet is the servlet to which the request needs to be dispatched.

41. What are include() and forward() methods?

  • forward() : Used to forward request to another resource.
dispatcher.forward(request,response);
  • include() : Includes the content of a resource(servlet, jsp page, html file etc) in the response.
dispatcher.include(request,response);

where request, response are HTTPServletRequest and HTTPServletResponse object and dispatcher is a RequestDispatcher object.

42. How to share value in servlet chain?

Values can be shared among the included/forwared servlets by setting attributes, we can set values as attributes in the request object and share between the servlets.
Request attribute is available only in the request scope which means the attribute is lost once the response is send to the user.

request.setAttribute(attributeName,value);

43. What are the steps to incude a servlet?

  • Step 1: Create the RequestDispatcher object
RequestDispatcher dispatcher = request.getRequestDispatcher(“SecondServlet”);
  • Step 2: Set request attributes if needed
  • Step 3: Call the include method to include the servlet
dispatcher.include(request,response);

44. Where to use include method in an application?

  • Include is used for reusing common code. Like a servlet to print the user login information in all the pages, the common servlet can be included in all the pages.
  • Use include if you want to present a collated response from a set of components(servlets,JSP etc).
  • Include can also be used for including static content to a page such as the page footer can be reused across all the pages in the application.

45. Where to use forward method in an application?

  • Forward is also used for code reusability.
  • Forward are typically used to forward a request to a success or error page after some processing. Example: After login credentials validated if success the control will be forwarded to home page else sent to error page.

46. What are the steps to forward a request?

  • Step 1 : Create the RequestDispatcher object
RequestDispatcher dispatcher = request.getRequestDispatcher(“SecondServlet”);
  • Step 2 : Set request attributes if needed.
  • Step 3 : call the forward method to forward request.

47. What happens if forward method is called after response is committed?

Forward should be called before response has been committed. If the response has already been committed, forward method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.

48. How Send Redirect works?

  • User requests a URL to the server.
  • Server sends the browser a status to redirect to a new URL.
  • Browser requests a new URL and sends a new request.

49. What are the disadvantages of Send Redirect?

  • It requires a round trip process.
  • The original request parameters are not included in the new HTTP request.

50. How forward method works?

  • User requests for a resource in server.
  • Server redirects the request to a new resource.
  • New resource sends the response to the client.

51. What are the advantages of forward method?

  • It works inside the servlet engine hence it is faster.
  • Forward is invisible to browser.
  • The original request received is preserved in the redirected resource.
Page 3 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

JSP Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers