what is the difference between a portlet and a servlet?

Rajesh picture Rajesh · Sep 26, 2009 · Viewed 90.5k times · Source

I am asked to work on portlets and portals.

I want to know the difference between a portlet and a servlet?

How / where does a portlet differ (may be functionally) from a servlet?

Answer

Ashok Goli picture Ashok Goli · Jan 19, 2012

Enhanced from Source: Servlets Vs Portlets

Similarities

  1. Servlets and Portlets are web based components which use Java for their implementation.

  2. Portlets are managed by a portlet container just like servlet is managed by servlet container.

  3. Both static and dynamic content can be generated by Portlets and Servlets.

  4. The life cycle of portlets and servlets is controlled by the container

  5. The client/server model is used for both servlets and portlets

  6. The packaging and deployment are essentially the same, WAR/EARs.

  7. Application Session exists in both Servlet and Portlet containers. It is one of the ways of of sharing data (crude Inter-Portlet Communication) from the render phase to the action phase (or any lower phases) in the portlet containers.

  8. Both Servlets and Portlets use similar server / VM environments that support it. Although, some additional configurations might needed in case of portlets to make it tick

  9. The build/DI tools are similar for both - Ant, Maven, Gradle, etc are all supported. Mostly :) - This has changed a bit with Liferay 7.


Dissimilarities

  1. Servlets can render complete web pages, whereas portlets renders html fragments. These fragments are aggregated by the portal into a complete web page.

  2. The content type of JSR 168 portlets can be only cHTML, XHTML, WML. It does not support other content types.

  3. Portlets are not allowed to generate HTML code that contains tags such as body, frame, frameset, head, html, or title.

  4. A Portlet unlike a servlet doesn’t have URL attached to it so it cannot be accessed directly. Access is only through the portal page which holds the portlet.

  5. Portlets can be provided with controls to manipulate its window states or portlet modes.

  6. Multiple instances of a single portlet can be placed onto the same page.

  7. Portlets support persistent configuration and customization, profile information.

  8. Portlets can have two types of request viz. render request and action request.

  9. Portlets have two scopes within session; application scope for communication across portlets and portlet scope for intra portlet communication.

  10. Portlet cannot set the character set encoding of the response nor can it set the HTTP response headers.

  11. Portlets doesn’t have access to request URL. So it cannot access the query parameters appended to the URL. Portlets cannot set cookies.

  12. Typical methods of Portlet API are doView(), doEdit(), doHelp() and processAction() while those of servlet are service(), doPost(), doGet().

  13. Servlet Specifications - JSR 369(Servlet 4.0), JSR 340(Servlet 3.1), JSR 315(Servlet 3.0), JSR 154(Servlet 2.5 & 2.4). Portlet Specifications - JSR 168(Portlet Spec v1.0), JSR 286(Portlet Spec v2.0), JSR 362(Portlet Spec v3.0)

  14. Deployment of Portlets involves different approach than a Servlet application. Some Providers (Liferay/Alfresco/WebSphere) support hot-deploying of portlets without the need to restart the server which is not possible in case of servlets without modularizing the application using special libraries such as OSGi.


Edit (From comments)

A Portlet container is built on a Servlet container. So ultimately it can be said that the portlet runs on a Servlet Container. But while developing apps, we view a portlet container separately from the Servlet/Java EE container.