Servlet

Recent Tutorial

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.

Difference between HttpServlet and GenericServlet

javax.servlet.GenericServlet Signature: 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. GenericServlet implements the log method, declared in the ServletContext […]
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.

Difference between ServletConfig and ServletContext

Signature: public interface ServletConfig ServletConfig is implemented by the servlet container to initialize a single servlet using init(). That is, you can pass initialization parameters to the servlet using the web.xml deployment descriptor. For understanding, this is similar to a constructor in a java class. Example code: <servlet> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.javapapers.ServletConfigTest</servlet-class> <init-param> <param-name>topic</param-name> <param-value>Difference between ServletConfig […]
Published to Servlet Tutorial on 06/05/2008.

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");
Published to Servlet Tutorial on 06/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, java.io.IOException  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.

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.
Published to Servlet Tutorial on 19/04/2008.