I need to hit a servlet whenever the author publishes any content on AEM. I need to do it at the end of the publish workflow such that the servlet is hit only after all the approval processes are complete and AEM finally replicates the content to the publish instance.
I need to do it for all ways of publishing content in AEM. Some of the most common ways are shown below (using AEM 6.5)
We have tried adding our own replication agent under "Agents on author" as shown below:
Pointed the agent to our custom servlet and the servlet is being hit but there seems to be absolutely no way to determine the page path from where the author has initiated publish. We require the page path inside the servlet too.
Therefore the only way seems to be through Jquery where on clicking the publish button an ajax call is made to the servlet and we are able to capture the current page path this way. This approach is incorrect since the ajax call is independent of the inbuilt AEM publish workflow and the servlet will be called even if the page publish is rejected by any approvers in the workflow.
The correct way would be to call the servlet at the end of the default AEM replication/activation/publish workflow but we also need the path of the page which was published/activated which seems impossible. Is there any way to call our servlet during the default AEM activation flow?
So what are trying to achieve? I understand it in a way, that you want to get the path of the page which is being published, is this correct? I ask because you mention at one point, that you need to extract the page the author is currently on. And while that might be the same in some cases, it's not required at all.
Then the next question: At which point do you need to your code to run? Before the replication is initiated? When the replication has completed? When the payload has reached all publishs?
A custom replication agent  that points to your servlet The request to your serlvet will contain the path to the activated element for the dispatcher flush agent (please refer to the headers of the HTTP request, namely the "CQ-Handle" header) or contain the full page content and information (in a serialized format) for the regular publication agent. If you just need the path, the dispatcher flush agent seems the better fit here. You can also write a custom replication agent. This agent is usually inactive on the author tier and only active for publish instances. But if you have a requirement for it to run on the author instance, there is no issue with that. Please note: replication agents are not supported for AEM as a Cloud Service.
A workflow launcher that listens on modifications of the cq:lastPublished property of your pages You can define a workflow launcher  that will listen to changes to the content and start a workflow whenever this happens. Your workflow could then either send the page (path) to your servlet or already hold the according logic that is currently implemented in your servlet. The launcher can be restricted to a certain path/content tree, to specific resourceTypes and only listen for changes to specific properties (the cq:lastPublication date in this case).
As you already mentioned, IMO it would not be an appropriate approach to handle this on the front end (jquery or similar).