Advanced Servlet Interview Questions and Answers

    1 Votes


Advanced Servlet Interview Questions and Answers for freshers and 2-6 year experienced java programmers asked by top MNC's like IBM, Oracle, Wipro, TCS, Infosys, Accenture and CTS.

1. How to set values in HTTP Session?

We can set values in session object using setAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
session.setAttribute(String attributeName, Object value);
Let's take a web mail application where user Name and location are stored.
HTTPSession session = request.getSession();
session.setAttribute(“User”,”Tim”);
session.setAttribute(“Location”,”Bangalore”);

 

2. How to retrieve attributes from session?
We can retrieve attributes from the session object using getAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
Object var = session.getAttribute(String attributeName);
Taking a web mail application where user Name and location are stored and retrieving the values.
HTTPSession session = request.getSession();
String userName = (String)session.getAttribute(“User”);
String location = (String)session.getAttribute(“Location”);
3. How to remove attributes from session?
Values can be removed from the session object using removeAttribute() method of the HTTPSession interface.
HTTPSession session = request.getSession();
session.removeAttribute(String attributeName);
Example: Taking a web mail application where user Name and location are stored. Assume we need to remove the userName attribute “Tim” from the session, the following API needs to be fired on HTTP request object.
HTTPSession session = request.getSession();
session.removeAttribute(“User”);
4. How to invalidate a session?

Session invalidating is the process of unbinding the session object from the user thereby removing all the previously stored data in the session and freeing the memory. To invalidate a session we call the invalidate() method.

5. When session invalidate() method is used?

This is typically used when user logs off from a web application to free up the memory utilized by the session object.

HTTPSession session = request.getSession();
Session.invalidate();

6. Why to avoid storing bulky objects in session?

Session objects are stored in server and it utilizes memory. So store only the needed information in sessions. Avoid bulky objects in session this can result in “out of memory error” resulting in a application crash.

7. What are the advantages of using HTTP Session?

Simple to use since it is managed in the server. It can function even when the cookie functionality is turned off in the browser.

8. What are the disadvantages of HTTP Session?

Since session objects are stored in the server the server memory required is more which is costly.

9. What are the attribute scopes in servlet?

Attribute scopes in servlet are:

  • Application scope
  • Session scope
  • Request scope

10. Compare between different attribute scopes in servlet?

Application scope:

  • It can be accessed by all servlet/JSP in the web application.
  • It is available for the life time of the application.
  • Achieved by storing in the ServletContext object.

Session scope:

  • It can be accessed by all servlet/JSP in the web application.
  • It is available for the life time of the session.
  • Achieved by storing in the HTTPSession object.

Request scope:

  • It is available to all servlet/JSP that have access to this specific request.
  • Available for the life of the request that is until response send to the client.
  • Achieved by storing in the HTTPServletRequest object.

11. What is a servlet event listener?

Listeners are used to perform some logic on trigger of certain events. Listeners are java classes that is used to implement the event handling mechanism. Example for Listener:

  • Listener class can be used for auditing user login information’s.
  • Listener classes can be used to clean up session objects and free memory when user logs off.

12. Classify the type of events in an web application?

There are three kind of events:

  • Servlet Context Events: These are events triggered when there is a change in the ServletContext object.
  • HTTP Session Events: These are events triggered when there is a change in the Session object.
  • Servlet Request Events: These are events triggered when there is a change in the request object.

13. When are events triggered?

The events are triggered under two circumstances:

  • Lifecycle changes: If there is a lifecycle change on the object. For example, when session created (or) initialized (or) destroyed.
  • Attribute changes: Any attribute stored /changed in the session. For example, when a attribute “message” is stored in the session.

14. Who triggers the events?

Whenever one of the changes in lifecycle or attribute happens the events are triggered by the web container which can be captured by listeners to perform a logic.

15. What are the steps to develop a servlet listener?

  • Step 1: Create the listener class. Listener class is done by implementing the required servlet listener interface by a normal java class and overriding all the methods.
  • Step 2: Listener class is registered with the deployment descriptor file.

16. What is a servlet filter?

Servlet filter are programs that runs on the server which intercepts HTTP request/response for transformations/processing/filtering.

17. How does filter work?

Steps of operation of a servlet filter:

  • Step 1: Intercepts the HTTP request and preprocess the request.
  • Step 2: Invokes the requested resource with the preprocessed HTTP request.
  • Step 3: Intercepts the HTTP response and transforms it.
  • Step 4: The transformed HTTP response will be sent back to the client.

18. What are the usage of filters?

  • Used for authenticating all HTTP request coming to the server. Example: All requests to the application will be authenticated and authorized against stored user credentials.
  • All the HTTP requests can be logged and audited in a flat file or database for tracking users of a web application. Example: The user credentials can be logged as “Ron logged in at 8:00 PM and he transferred 10,000 dollars from the account.
  • To perform some formatting of HTTP response, say display a message in the header (or) bread crumbs for all the pages of the application. Example: All the HTML pages displayed in the client should have the navigation breadcrumb like “Home Page> Savings Account> Account Summary”

19. Name and define important filter interfaces?

  • Filter: Contains the methods for filtering the request/response.
  • FilterChain: Contains the methods for chaining the filters.
  • FilterConfig: It is an object used by a servlet container to pass information to a filter during initialization.

20. What are the steps to create a filter?

  • Step 1: Create a class that implements the filter interface.
  • Step 2: Implement the doFilter method with the pre/post processing logic.
  • Step 3: Invoke the doFilter method of the FilterChain object.
  • Step 4: Register the filter with the appropriate servlets.
Page 1 of 4

Related Articles

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

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know