Friday, 3 May 2013

Comparing Servlets and Portlets Part 1 : Understanding the similarities and differences

People familiar with Servlet technology will be able to relate to Portlets. The concepts are more or less the same for both the technologies.

Lets do a quick walkthrough:

To start with the lets talk about their containers.

A container provides the environment in which the application can run. For running a servlet we will need a web application container like Tomcat.
On similar lines for a Portlet we need a Portlet Container. Most of the Portals today come with an inbuilt Portlet container to serve requests.

A container helps you to focus on the application rather than focus on issues like multi-threading and socket connections. And lets not forget, the container manages the lifecycle events.

Below is a summary of the lifecycle methods for a Servlet and a Portlet.

Servlet Lifecycle sequence:

1. Container loads the Servlet class.
2. Container instantiates the Servlet by invoking the default constructor.
3. Calls the init method, on the first client request, and provides essential objects like ServletConfig and ServletContext
4. Calls the service method to handle the request.
5. Calls the destroy method to finally put all the resources eligible for GC.

Portlet Lifecycle sequence: All the steps are more or less the same except for step 4.

1. Container loads the Servlet class
2. Container instantiates the Servlet using super constructor
3. Calls init, on the first client request, and provides essential objects like PortletConfig and PortletContext
4. Calls action or render to handle the request. Note in this step the container will either call action and then render or just call render.
   We will discuss more about this step. Unlike Servlets which does this in one phase a Portlet has well defined phases for this step. There can be
   other methods also in this phase like resource and event.
5. Calls the destroy method to finally put all the resources eligible for GC.

HttpServlet vs GenericPortlet


For creating a Servlet class we can implement Servlet interface, which has the lifecycle methods, or we can extend the well known HttpServlet class.
Which lets us choose the request methods we want to extend. For example:

public class SampleServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) {
//Logic goes here

HttpServlet will figure out which method to call based on the request.


For creating a Portlet we can again either implement the Portlet interface which has the lifecycle methods or extend the convenient GenericPortlet class.
Now like the HttpServlet class GenericPortlet class also helps us to only extend the methods we need. For example:

public class MainController extends GenericPortlet{
public void doGet(RenderRequest request, RenderResponse response){
//Logic goes here

In the next article we will carry this discussion forward and look at more similarities and differences between a Servlet and a Portlet. Like the config files.

No comments:

Post a Comment