Access Custom Servlet using Portal Delegate Servlet – Liferay

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 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) { 
 } catch (SystemException e) { 

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="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="2.5"> 

Now we can access our custom servlet using following URL:


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


Also Read: