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 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(); 
 }     
}
}

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="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 the following URL.

http://localhost:8080/delegate/CustomServlet

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

custom servlet liferay

Conclusion

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

Accessing Custom Servlet In Liferay
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading