How to create a high-level servlet in liferay

I wanted to create a servlet in liferay that listens for a url like

http://localhost:8080/my-servlet 

I tried adding it to the portlet, but I have a URL

 http://localhost:8080/my-portlet/my-servlet 

I tried adding a servlet description to web.xml ext-web, but no luck. Is there any way to add such a servlet?

+7
java servlets liferay liferay-6
source share
3 answers

Liferay is also a "Servlet" -Application - but very very large. And Liferay needs some servlet container like tomcat, jetty, jboss, etc.

However, you can simply create a servlet project and deploy it directly to the servlet container where liferay runs.

edit: and place it in web.xml using the direct access servlet mapping, for example, "/ *".

+5
source share

If you want to access the Liferay API, you can use PortalDelegateServlet: adding the following to your web.xml:

 <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class> <init-param> <param-name>servlet-class</param-name> <param-value>org.example.MyServlet</param-value> </init-param> <init-param> <param-name>sub-context</param-name> <param-value>myservlet</param-value> </init-param> </servlet> 

will make your servlert accessible through

http://example.org/delegate/myservlet

in your servlet class, then you do things like retrieve the registered user and check permissions:

 package org.example; public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { User user = PortalUtil.getUser(request); PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user); ... 
+10
source share

Follow this sample to learn how to create a servlet path in the liferay plugin:

https://github.com/liferay/liferay-plugins/tree/master/hooks/sample-servlet-filter-hook

+4
source share

All Articles