Accessing Custom Servlet In Liferay

While developing a web application using Liferay, sometimes we may want to use a custom servlet and deploy it on our Liferay portal. Also, we may need to access the portal’s session parameters inside our custom servlet. We can achieve this using Liferay’s Portal Delegate Servlet.

In this article, we will learn how to access our custom servlet deployed on the Liferay portal.

We will also learn how we can achieve this using the Liferay Portal Delegate Servlet.

Create a Liferay Plugin project and add a new servlet

Create a new Liferay plugin project from the option: 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) { 

Our custom servlet class above retrieves the User details from the portal session and prints them to system output.

Add the required servlet configuration

To access our custom servlet, we need to add the below 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 the following URL.


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

custom servlet liferay


In this article, we learned how to access custom servlets using the portal delegate servlet in the Liferay application.