Servlet vs Portlet

Servlet vs Portlet is curious topic for Java developers to know in details. Servlet and Portlet are web component used to generate content for user request. They are used in different context. It’s easy to understand the functionality of portlet and servlet by comparing characteristics of Servlet vs Portlet
Almost all Java developer are familiar with Servlet. There are many similarities (and differences) between Servlet vs Portlet. So it is easy to understand the difference between Portlet and Servlet by comparing them side by side with different characteristics.

 Servlet vs Portlet

  • Component
    • like Servlet, Portlet is web component.
  • Packaging and Deployment in Servlet vs Portlet
    • Servlet is deployed as .war file to Servlet container.
    • Portlet is deployed as .war file to Portlet container( Ex. Liferay, WebSphere Portal, Oracle Portal etc ). Portlet container ultimately runs on application server or Servlet container.
    • For Servlet container,each Portlet is separate web application.
    • Since Portlets are deployed to Servlet container as a web application, they must provide valid web.xml file like Servlet.
    • Liferay provides process called hot deployment. In this process .war file needs to be placed under specific folder (most of the cases ‘deploy‘ folder under Liferay-Tomcat bundle folder),and Liferay will pick up and deploy it to server. This process will take care of creating / merging web.xml file if we have not created during creating of Portlet. In other words, hot deployment process will create web.xml before deploying portlet to Servlet Container.
  • Deployment Descriptor of Servlet vs Portlet
    • In web application, multiple servlets can be defined in web.xml. so web.xml is deployment descriptor for Servlet.
    • Portlet specification added additional descriptor file called ‘portlet.xml‘ apart from web.xml
    • portlet.xml file defines various elements like 
      • portlet name
      • portlet description
      • portlet class
      • portlet modes
      • init parameters
      • portlet information
      • roles given to portlet etc
    • portlet.xml can contain more than one portlet’s information in separate <portlet> element
    • You can download xsd for portlet.xml to know complete list of elements covered.
  • Personalization in Servlet vs Portlet
    • Servlet is responsible to generate whole page content.
    • Portlet is designed to generate small portion of whole page. It also allow to make the generated content personalized.
    • Personalization in portlet is achieved through portlet preference.
    • User can set different portlet preference and portlet may generate different content based on preference set.
    • Personalization is not possible for servlet because it will generate same content for all user.
  • Content Generation in Servlet vs Portlet
    • Servlet is designed to generate whole page content
    • While Portlet is designed to produce only some portion of the total page.
    • In other words, Portlets are design to provide aggregate content on page 
    • Because of this behavior, certain html elements like <html><body> etc are not allowed in portlet’s JSP. However we can still put such element but then there will be multiple occurrence of such element which is not a valid HTML document and its not best practice. 
  • URLs of Servlet vs Portlet
    • Servlet’s mapping URLs are defined in <servlet-mapping> element of web.xml and we can predict Servlet’s URL at development time.
    • While developing portlet, its not decided on which page it get deployed and what other portlets are placed along with it on that page. So practically its impossible to create / pradict portlet URL statically. 
    • However, Portlet API provides way to build Portlet URLs. Below are different kinds of Portlet URL available. 
      • Render URL
        • used to call render method of portlet
      • Action URL
        • used to call action method of portelt
      • Resource URL
        • used to call serve resource method of portlet
    • We can create above Portlet URL by Java API or by liferay tags in JSP or by java script by AUI moduels.
  • Request & Response Objects in Servlet vs Portlet
    • Servlet has direct access to ServletRequest and ServletResponse objectsbut Portlet doesn’t have that. Because of this, portlet can’t directly set character encoding or set headers in response.
    • Portlet will have access to separate object called PortletRequest PortletResponse. These interfaces are further extended by RenderRequestActionRequest and ResourceRequest as per below screenshot.

Servlet vs Portlet - PortletRequestRenderRequest


    • Similarly hierarchy is available for Response object.
    • RenderRequest and RenderResponse objects are passed in render method
    • ActionRequest and ActionResponse objects are passed in action method
    • ResourceRequest and ResourceResponse objects are passed in Resource method.
  • URL Parameters in Servlet vs Portlet
    • Servlet can read query parameter but Portlet can’t read it directly because Portlet don’t have direct access of ServletRequest object.
    • As per portlet specification, Portlet can read parameters from its own URL or the one which are defined as Public Render Parameter in portlet.xml file. 
      • Example of Parameters in Portlet URL
        • Explanation:-
          • in above Portlet URL, following parameters are accessible by Portlet
            • p_auth
            • p_p_id
            • p_p_lifecycle
            • p_p_state
            • p_p_col_id
            • p_p_col_pos
            • p_p_count
            • javax.portlet.action
      • Example of Query Parameter
          • in above URL, following are query parameter
            • firstName
            • lastName
      • At first look, there is no difference between above two urls. But PortletURLs are dynamically generated based on portlet mode, its position, its state, column Id of layout(on which the portlet is placed) etc. 
      • Second url can by typed from browser directly and servlet can read parameter but portlet specification doesn’t provide facility to read query parameter directly. 
    • However, liferay provides certain Util methods which will help to get ServletRequest from PortletRequest and able to read query parameter. 
    • These Util methods are Liferay specific. If such Util methods are used in portlet, then those portlets are only work in Liferay and not on other portlet container.
  • Mode in Servlet vs Portlet
    • Portlet provides various modes like view, edit , help etc. 
    • while servlet doesn’t have such modes.
  • Lifecycle of Servlet vs Portlet
    • Portlet serves user request by different methods like render(), processAction(), serveResource(), processEvent() etc. 
    • while Servlet serves user request through service() method only. 
  • Window State in Servlet vs Portlet
    • Portlet is designed to generate only some portion of page. Multiple portlets can be placed on single page. Portlet specification provides window state so that portlets can be rendered in Maximized, Minimized or Normal window state.
    • Servlet specification not having such window state since its used to generate whole page content.

Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.