Servlet interview questions

Explain the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

In request.getRequestDispatcher(path) in order to create it we need to give the relative path of the resource. But in resourcecontext.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.

Explain the difference between using  getSession(false) and getSession(true) methods?

getSession(true) – This method will check whether already a session is existing for the user. If a session is existing, it will return that session object, Otherwise it will create new session object and return taht object.
getSession(false) – This method will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.

Explain on what is servlet mapping?

The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets.

Is there a way by which we can use constructor, instead of init(), to initialize servlet?

Yes. But you will not get the servlet specific things from constructor. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no- arg constructor. So you won’t have access to a ServletConfig or ServletContext.

Which are the different ways you can communicate between servlets?

Below are the different ways of communicating between servlets:-

  • Using RequestDispatcher object.
  • Sharing resource using “ServletContext ()” object.
  • Include response of the resource in the response of the servlet.
  • Calling public methods of the resource.
  • Servlet chaining.

Explain the difference between Authentication and authorization?

Authentication is the process the application identifies that you are who. For example when a user logs into an application with a username and password, application checks that the entered credentials against its user data store and responds with success or failure. Authorization, on the other hand, is when the application checks to see if you’re allowed to do something. For instance are you allowed to do delete or modify a resource.application and thus can be used to share data between
servlets.

What is Server Side Includes (SSI) ?Explain in detail

Server Side Includes (SSI) are commands and directives placed in Web pages that are evaluated by the Web server when the Web page is being served. SSI are not supported by all web servers. So before using SSI read the web server documentation for the support. SSI is useful when you want a small part of the page to be dynamically generated rather than loading the whole page again.