While developing web application using Liferay, sometimes we may want to use custom servlet and deploy it on our liferay portal. Also we may want to access portal session related parameters inside our custom servlet. This can be achieved using liferay’s Portal Delegate Servlet.

In this article, we will learn how to use Liferay’s Portal Delegate Servlet to access our custom servlet deployed on liferay portal. Following are the required steps.

  • Create a Liferay Plugin project and add a new servlet.
  • Add the required servlet configuration.

Create a Liferay Plugin project and add a new servlet

Create a new Liferay plugin project from File > New > Liferay Plugin Project and add a servlet from File > New > Servlet. We can access portal session related parameters within this servlet.

package com.asb;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CustomServlet extends HttpServlet { 
 private static final long serialVersionUID = 1L;        
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
this.doPost(request, response); }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
 System.out.println("Inside post method!!"); 
 try { 
  User user = PortalUtil.getUser(request); 
  System.out.println("User Details::" + user.getFirstName() + " "+ user.getLastName()); 
 } catch (PortalException e) { 
  e.printStackTrace(); 
 } catch (SystemException e) { 
  e.printStackTrace(); 
 }     
}
}

In our custom servlet class above, we are getting User details from portal session and printing it to system output.

Add the required servlet configuration

To access our custom servlet, we can have to add the following servlet configuration into our plugin project’s web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
<servlet> 
 <servlet-name>CustomServlet</servlet-name>    
 <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>     
 <init-param>       
  <param-name>servlet-class</param-name>       
  <param-value>com.asb.CustomServlet</param-value>     
 </init-param>    
 <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>    
  <servlet-name>CustomServlet</servlet-name>    
  <url-pattern>/CustomServlet</url-pattern>  
</servlet-mapping>
</web-app>

Now we can access our custom servlet using following URL:

http://localhost:8080/delegate/CustomServlet

On hitting the above URL, our custom servlet prints User’s first and last name, if user session exists.

PortalDelegateServlet

Also Read: