Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

java - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...