Servlet filter provides a filter layer between the client and the servlet. When the client invokes the request, at that time servlet filter will perform the pre-process steps to filter the request before completing the request, also it will perform the post-processing steps before sending it to the client.

Here are some common filter usage cases.

  • Logging
  • Auditing
  • Transaction management
  • Security

1. Create a module project.

  1. Go to Liferay workspace project modules new.
  2. Select other Liferay Liferay Module Project and Click on “Next”.
  3. Enter the project name.
  4. Select “Project Template Name” as “war-hook” and click on “Next”.
  1. Enter a Package name and click on the “Finish”. The necessary file structure will be created as below.

2. Create a servlet filter class and implement with the Filter class.

Filter interface provides the following life cycle methods.

  • init(): It is used to initialize the filter.
  • doFilter(): It is used to perform filtering tasks.
  • destroy(): Clean up the filter’s unneeded resources.

3. Add below mapping in Liferay-hook.xml.

4. Now, you can deploy it in the Liferay server and observe the logs in the terminal(cmd).