Advanced Servlet Interview Questions and Answers - Page 2

    1 Votes

21. What is doFilter method?

public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws ServletException, IOException

This method is executed every time when a servlet (or) JSP associated with this filter is invoked. This contains the filtering or pre and post processing logic.

Method Arguments:
  • ServletRequest: Request object of the incoming request to be intercepted and processed.
  • ServletResponse: Response object of the outgoing response to be intercepted and processed.
  • FilterChain: The filter chain object on which the doFilter method of the FilterChain interface can be called.
22. What is FilterChain interface?

Multiple filters can be chained and invoked one by one this is achieved using the FilterChain interface. Filter uses the FilterChain to invoke the next filter in the chain or if the calling filter is the last filter in the chain it will invoke the resource at the end of the chain. FilterChain contains only one method, void doFilter(ServletRequest request,ServletResponse response).

23. Why do we override doFilter method?

It is overridden to perform one or more of the following tasks:

  • Transform the incoming request.
  • Transform the response before being sent to the client.
  • It invokes the doFilter method of the FilterChain interface.

24. What are the two elements used for declaring filters in web.xml file?

Web.xml uses two elements for declaring filters,

  • Filter - Registers a filtering object with the system.
  • Filter-mapping - Specifies the URLs to which the filtering object applies.

25. What is filter chaining?

Filter chaining is the process of applying more than one filter to a servlet.

26. How is filter chaining configured?

This is done by configuring more than one filter mapping for a servlet in the deployment descriptor file. The filters are invoked in the order they are declared in the deployment descriptor file.

27. What is a web application?

Web application is a collection of components assembled to provide services over the web for users to consume the service provided. Example: online train ticket reservation, internet banking.

28. How are web application accessed?

Web applications are accessed over the web using URL address. URL stands for Uniform Resource Locator which is a unique identifier available to locate a particular service.

29. How HTTP works?

HTTP stands for Hypertext Transfer Protocol. The following below steps explains about working:

  • Clients opens connection.
  • Clients sends HTTP request.
  • Server sends HTTP response.
  • Client closes connection.

30. Why HTTP is called stateless protocol?

Since the connection is open and closed for each HTTP request, the state is not maintained across HTTP requests. Hence HTTP is called “Stateless Protocol”.

31. Where do web application live?

Web application are deployed and managed in web or application server.

32. What is the difference between web and application server?

Web server is a software which provides services to host web applications which can be accessed via HTTP protocol. Example: Application built using servlet/JSP only. Application server or Java EE server is a software which supports application built over HTTP and other protocols such as IIOP,RMI,JMS. It also provides services to manage application built using JDBC transaction, JNDI tree, EJB, LDAP, JMS etc.

33. How are web application deployed?

A web application to be deployed in web/application server it needs to be packaged and deployed in a specific format which can be recognized by the servers. The packaging format is web archive format(war) used for packaging web application containing servlets, JSP,HTML,image files or other web components.

34. How MVC works?

  • Step 1: The controller servlet handles the user’s request.
  • Step 2: The controller servlet then invokes the model and performs the business logic or data retrieval.
  • Step 3: The model returns back the data (response) back to the controller.
  • Step 4: The controller then transfers the data to the view.
  • Step 5: The view renders the data in the appropriate format requested by the client.

35. What are the benefits of MVC?

  • Clear separation of layers, modular and easy to maintain. Example: The model holds business logic or data manipulation logic and view holds the presentation logic. System is modular and easy to maintain as developer know which logic is present in which layer.
  • Supports parallel development so reduces development time. Example: The view and model can be developed by different teams and finally integrated. This reduces development time to market.
  • Flexible for future enhancements. Example: Any changes done like database upgradation or change in business logic it is enough if the model is changed without changing the view or controller.

36. What are the features of servlets?

  • Security: A Web container provides a runtime environment for executing a servlet. Servlets inherit the security feature provided by the web container. This allows developers to focus on the servlet functionality and leave the security issues to the Web container to handle.
  • Session management: It is the mechanism of tracking the state of a user across multiple requests. A session maintains the client identity and state across multiple requests.
  • Instance persistence: Servlets help to enhance the performance of the server by preventing frequent disk access. For example, if a customer logs on to an online banking site, the customer can perform activities, such as checking for the balance or applying for a loan. The account number of the customer will be validated at every stage from the database. Instead of every time checking the account number against the database, servlets retain the account number in the memory till the user logs out of the Web site.
  • It is platform and server independent.

37. What is the difference between HTTP Get vs HTTP Post?

HTTP Get:

  • 1. User entered information is appended in a URL string.
  • 2. It can send only a limited amount of data.
  • 3. The browser caches the data.

HTTP Post:

  • 1. User entered information is send as data as a part of the message body not appended to URL.
  • 2. It can send any amount of data.
  • 3. The data’s are not cached.

38. What is get method usage?

If the processing of a form is idempotent (that is it has no side effect on the state of the database), then the form method should be GET. Many database searches have no side-effects and make ideal applications of query forms. GET method can only populate 255 characters.

Page 2 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
Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers