Servlet

Difference between ServletRequest.getRequestDispatcher and ServletContext.getRequestDispatcher

request.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request. Example code: RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp"); getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext. Example code:RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");

ServletRequest and ServletResponse – Explain

ServletRequest and ServletResponse are two interfaces that serve as the backbone of servlet technology implementation. They belong to the javax.servlet package. Signature: public interface ServletRequest Blueprint of an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and sends it as an argument to the servlet’s service method. Signature: public interface [...]

Why not declare a constructor in servlet?

Technically you can define constructors in servlet. But, the declared constructor cannot access the ServletConfig object or throw a ServletException. Then why is it not customary to declare a constructor in a servlet? Because the init() method is used to perform servlet initialization. In JDK 1.0 (servlet were written in this version), constructors for dynamically loaded Java classes [...]

Servlet Life Cycle – Explain

Servlet Life Cycle: The interface javax.servlet.Servlet defines the following three methods known as servlet life cycle methods.  public void init(ServletConfig config) throws ServletException  public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException  public void destroy() Creation and intialization The container first creates the servlet instance and then executes the init() method.  init() can be called only once in its life cycle by [...]

What is a filter?

A filter is used to dynamically intercept request and response objects and change or use the data present in them. Filters should be configured in the web deployment descriptor. Filters can perform essential functions like authentication blocking, logging, content display style conversion, etc.