Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a top-level servlet in liferay

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 ?

like image 613
Breiti Avatar asked Jan 06 '14 13:01

Breiti


People also ask

What is servlet in Liferay?

Java Servlets are foundational to Java EE. You can use servlets and servlet filters to provide applications in your portal context and to process requests and responses to specific URLs in your sites. The tutorials here cover servlet technology and how it integrates with Liferay DXP.

Who will instantiate the servlet?

After the web container loads and instantiates the servlet class and before it delivers requests from clients, the web container initializes the servlet.


2 Answers

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);
    ...
like image 103
Alain Dresse Avatar answered Sep 21 '22 14:09

Alain Dresse


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 "/*".

like image 27
Mark Avatar answered Sep 21 '22 14:09

Mark