How to create a top-level servlet in liferay
Asked Answered
L

2

7

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 but the I have the URL

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

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

Loseff answered 6/1, 2014 at 13:5 Comment(5)
Cant you create web project and deploy on liferay tomcat?Denitrify
Will this be top-level or will it prefix the url with it´s own name ?Loseff
It will be prefix with project nameDenitrify
yea, thanks for your answer but I really want it to be top-level.Loseff
Follow this sample to learn how to create a servlet path in a liferay plugin: github.com/liferay/liferay-plugins/tree/master/hooks/…Mellott
Z
5

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

However, you can simple create servlet project and deploy it direct to servlet container where liferay is running.

edit: and put to web.xml by servlet-mapping a direct access like "/*".

Zoology answered 6/1, 2014 at 21:2 Comment(1)
That´s true. Sometimes the solutions are really simple: portal-service.jar can still be used ... :)Loseff
M
11

If you want to access Liferay service API, you may consider using 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 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);
    ...
Minefield answered 10/1, 2014 at 10:28 Comment(2)
sounds fantastic ! Thanks for the hintLoseff
how to prevent "not found page" when access to url??Cerussite
Z
5

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

However, you can simple create servlet project and deploy it direct to servlet container where liferay is running.

edit: and put to web.xml by servlet-mapping a direct access like "/*".

Zoology answered 6/1, 2014 at 21:2 Comment(1)
That´s true. Sometimes the solutions are really simple: portal-service.jar can still be used ... :)Loseff

© 2022 - 2024 — McMap. All rights reserved.