Servlet Basics

Recent Tutorial

What happens if you call destroy() from init() in java servlet?

destroy() gets executed and the initialization process continues. It is a trick question in servlets interview. In java servlet, destroy() is not supposed to be called by the programmer. But, if it is invoked, it gets executed. The implicit question is, will the servlet get destroyed? No, it will not. destroy() method is not supposed […]
Published to Servlet Tutorial on 27/05/2008.

How to avoid IllegalStateException in java servlet?

The root cause of IllegalStateException exception is a java servlet is attempting to write to the output stream (response) after the response has been committed. It is always better to ensure that no content is added to the response after the forward or redirect is done to avoid IllegalStateException. It can be done by including […]
Published to Servlet Tutorial on 19/05/2008.

What is servlet mapping?

Servlet mapping specifies the web container of which java servlet should be invoked for a url given by client. It maps url patterns to servlets. When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets. How is […]
Published to Servlet Tutorial on 14/05/2008.

What is preinitialization of a java 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 three ways. The default way is that, the java servlet is initialized when the servlet is called for the first […]
Published to Servlet Tutorial on 14/05/2008.

ServletRequest vs ServletResponse

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. […]
Published to Servlet Tutorial on 06/05/2008.

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 […]
Published to Servlet Tutorial on 06/05/2008.

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,  public void destroy() Creation and initialization The container first creates the servlet instance and then executes the init() method.  init() can be called only once in […]
Published to Servlet Tutorial on 05/05/2008.