How to create a top-level servlet in liferay

后端 未结 2 655
北恋
北恋 2020-12-19 06:22

I wanted to create a servlet in liferay that is listening to a URL such as

http://localhost:8080/my-servlet

I tried to add it to a portlet

2条回答
  •  一向
    一向 (楼主)
    2020-12-19 06:50

    If you want to access Liferay service API, you may consider using PortalDelegateServlet : adding the following to your web.xml:

    
        myServlet
        com.liferay.portal.kernel.servlet.PortalDelegateServlet
        
            servlet-class
            org.example.MyServlet
        
        
            sub-context
            myservlet
        
    
    

    will make your servelt accessible through

    http://example.org/delegate/myservlet

    in your servlet class, you then do things like extract the logged-in 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);
        ...
    

提交回复
热议问题