Why not to write 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 such as servlets cannot accept arguments. Therefore init() was used to initialize by passing the implemented object of ServletConfig interface and other needed parameters.
Also, Java constructors cannot be declared in interfaces. So, javax.servlet.Servlet interface cannot have a constructor that accepts a ServletConfig parameter. To overcome this, init() method is used for initialization instead of declaring a constructor.
What is difference in ServletRequest.getRequestDispather() and ServletContext.getRequestDispatcher() in Servlet
Servletrequest.getRequestDispatcher(“url”) means the dispatch is relative to the current HTTP request.
RequestDispatcher reqDispObj = request.getRequestDispatcher("/login.jsp");
getServletContext().getRequestDispatcher(“url”) means the dispatch is relative to the root of the ServletContext.
RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/testApp/login.jsp");
What is difference between servletContext and ServletConfig?
ServletConfig is implemented by the servlet container to initialize a single servlet using init(). you can pass initialization parameters to the servlet using the web.xml deployment descriptor. Every servlet has its own ServletConfig object.
Continue reading “Difference between servletContext and ServletConfig”
What is preinitialization of servlet
In the java servlet life cycle, the first phase is called “Creation and intialization“.
The java servlet container first creates the servlet instance and then executes the init() method. This initialization can be done in Two ways. The default way is that, the java servlet is initialized when the servlet is called for the first time. This type of servlet initialization is called lazy loading.
Continue reading “What is preinitialization of servlet”
How to set the Session Time out in JSP and Servlet
Session can die in following three ways in J2EE application:
- Its Time out
- invalidate() method called on Session Object
- Application goes down (Application crashes or undeployed)
Continue reading “Setting Session Timeout in JSP Servlet”
What is the basic Difference in HttpServlet and GenericServlet
public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
- GenericServlet defines a generic, protocol-independent servlet.
- GenericServlet gives a blueprint and makes writing servlet easier.
- GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.
- To write a generic servlet, it is sufficient to override the abstract service method.
Continue reading “Difference in HttpServlet and GenericServlet”