Expand my Community achievements bar.

SOLVED

Default Sling Servlet

Avatar

Level 6

Hi

My Logout Servlet is defined as

 

@Component(enabled = true)
@Service(Servlet.class)
@Properties({
    @Property(name="resourceTypes", value="sling/servlet/default"),
    @Property(name="methods", value="POST"),
    @Property(name="selectors", value="ourlogout"),
    @Property(name="extensions", value="html"),
    @Property(name="service.pid", value="package.LogoutServlet", propertyPrivate=false),
})

 

After compaling and using VLT to commit changes to the repository.  i see it in http://localhost:4502/system/console/components

but not in http://localhost:4502/system/console/services

 

what is problem here?

How else I can test from Adobe AEM this default Sling Servlet ?

1 Accepted Solution

Avatar

Correct answer by
Level 10

Here is a good documentation for sling servlets: [1] and Read this as well [2]

You are able to see it  because you have enabled (@Component(enabled = true))  .

You can access this servlet via path which you have given or properties which you have defined.

Example. A servlet defined as 

@Service(Simple.class) @SlingServlet(paths = { "/bin/simple/adobe" }, generateComponent = false) @Component(label = "Simple adobe component", enabled = true, immediate = true, metatype = false) public class Simple extends SlingSafeMethodsServlet { }

Will be accessed via path 

/bin/simple/adobe

 

[1] https://sling.apache.org/documentation/the-sling-engine/servlets.html

[2] http://stackoverflow.com/questions/8886430/what-is-the-difference-between-osgi-components-and-servic...

View solution in original post

17 Replies

Avatar

Employee

Why are you not using a maven project to build and deploy your servlet? I haven't heard of using vlt to commit code changes to AEM.

[1]https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Avatar

Correct answer by
Level 10

Here is a good documentation for sling servlets: [1] and Read this as well [2]

You are able to see it  because you have enabled (@Component(enabled = true))  .

You can access this servlet via path which you have given or properties which you have defined.

Example. A servlet defined as 

@Service(Simple.class) @SlingServlet(paths = { "/bin/simple/adobe" }, generateComponent = false) @Component(label = "Simple adobe component", enabled = true, immediate = true, metatype = false) public class Simple extends SlingSafeMethodsServlet { }

Will be accessed via path 

/bin/simple/adobe

 

[1] https://sling.apache.org/documentation/the-sling-engine/servlets.html

[2] http://stackoverflow.com/questions/8886430/what-is-the-difference-between-osgi-components-and-servic...

Avatar

Level 10
When invoking your servlet, are you seeing anything in log any error?       

Avatar

Level 10

For a logout - i would have a button and when its clicked - i would invoke a Sling Servlet by using binding by path. 

As shown in this Sling topic - both are valid: https://sling.apache.org/documentation/the-sling-engine/servlets.html.

However - if you want to bind the servlet by resource type - here is an article:

Binding Adobe Experience Manager Servlets to ResourceTypes @ https://helpx.adobe.com/experience-manager/using/resourcetypes.html.

Now for using the vault tool - you use that to sync code between your IDE ( for example, Eclipse) and the JCR - not to build and deploy servlets - you use Maven for that - as shown in the above article

Avatar

Level 6

thank you, @edubey

Meanwhile I was reading about Servlets called by resource type and default servlet.

Even though I can see this servlet in Components, and in the 2nd link you provided, it is said : "A component may publish itself as an OSGi service."

in publish instance "Logout" button is not working.

The post action just changes the URL to:   /content/<site>/<path>/<html_name>.ourlogout.html

But the logout servlet hasn't been called.

Where did I leave something?

Avatar

Level 6

smacdonald2008 wrote...

For a logout - i would have a button and when its clicked - i would invoke a Sling Servlet by using binding by path. 

As shown in this Sling topic - both are valid: https://sling.apache.org/documentation/the-sling-engine/servlets.html.

However - if you want to bind the servlet by resource type - here is an article:

Binding Adobe Experience Manager Servlets to ResourceTypes @ https://helpx.adobe.com/experience-manager/using/resourcetypes.html.

Now for using the vault tool - you use that to sync code between your IDE ( for example, Eclipse) and the JCR - not to build and deploy servlets - you use Maven for that - as shown in the above article

 

 


thank you.

I can do that

But problem is that this Servlet is not listed in Services.

here

https://forums.adobe.com/thread/1113958

 

It is written "ExampleServlet must be visible in the bundle in the Services list"

But my servlet is inside the whole project JAR ( OSGI-INF)

Maybe i am doing something wrong.

 

This servlet is visible in Components only.

Avatar

Level 6

Opkar Gill wrote...

Why are you not using a maven project to build and deploy your servlet? I haven't heard of using vlt to commit code changes to AEM.

[1]https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

 


yes I use Maven.... 

https://docs.adobe.com/docs/en/cq/5-5/developing/developmenttools/developing_with_eclipse.html

here it is written to use VLT too.

Avatar

Level 6

edubey wrote...

When invoking your servlet, are you seeing anything in log any error?

 


how to invoke the default servlet?

In 4503 on clicking  logout button no traces in log.

Avatar

Level 6

I read this page even before asking  custom-sling-servlets.html

But they use paths of the servlet.

But I would like to know why my logout doeas't work  with this default servlet with resource type.

Avatar

Level 10

WHen you define a servlet - you should not be using @Component and @Service. For a Servlet - use @SlingServlet:

@SlingServlet(methods = {"GET"},
    metatype = true,
    resourceTypes = {"services/powerproxy"},
    selectors = {"groups"})

Avatar

Level 6

smacdonald2008 wrote...

WHen you define a servlet - you should not be using @Component and @Service. For a Servlet - use @SlingServlet:

@SlingServlet (methods = { "GET" },
     metatype = true ,
     resourceTypes = { "services/powerproxy" },
     selectors = { "groups" })

 


I can't define sling:resourceType in jcr:component as it is only for pages, but Logout button is inside component.

 

I will try a servlet with PATH now.

Avatar

Level 10

just wondering, can you show the doPost() method you implemented in your servlet?

Avatar

Level 6

Hi,

do you remember I was telling that I am exporting the working site to the test server. So when i imported all the packaged this logout was working.

After I worked on some tasks to improve some components, compiled the project, this logout stopped working.

Here you go:

 

public class LogoutServlet extends SlingAllMethodsServlet {

    private static final long serialVersionUID = ... ;

    @Reference
    private Authenticator authenticator;

    public LogoutServlet() {}

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        Authenticator authenticator = this.authenticator;
        if (authenticator != null) {
            try {
                String resourcePath = request.getParameter("resource");
                request.setAttribute("resource", resourcePath != null ? resourcePath : "/");


                eraseCookies(request, response);

                authenticator.logout(request, response);
                return;
            } catch (IllegalStateException ise) {
                log.error("service: Response already committed, cannot logout");
                return;
            }
        }

        log.error("service: Authenticator service missing, cannot logout");

        response.setStatus(204);
    }

Avatar

Level 10

Define the servlet by path and call it when a user clicks the logout button.

That is - when the button is clicked, send an AJAX request to invoke the servlet (the community article at the start of this thread shows how to use AJAX to invoke a Servlet).

The servlet will be called and the app logic will log out the user. 

Avatar

Level 10

Yes, I am remember you were setting up code that time. 

By any chance, can you compare code of both the instance after you made changes as you mentioned might have affected.

Avatar

Level 6

smacdonald2008 wrote...

Define the servlet by path and call it when a user clicks the logout button.

That is - when the button is clicked, send an AJAX request to invoke the servlet (the community article at the start of this thread shows how to use AJAX to invoke a Servlet).

The servlet will be called and the app logic will log out the user. 

 


yes, thank you for all the comments and links, I am changing this code now. But it is curious how the previous developer archived it with default servlet. PS: can't talk to him.

Avatar

Level 6

Hello, guys.

After  changing Servlet to work with path, then adding some additional JS in JSP

and changing default login and logout path in system console (otherwise was going to Geometrix)

following this post:

https://saraindia.wordpress.com/2012/08/09/logout-on-a-publish-instance/

My logout worked.

Thank you, all.

******************************************************************

PS: still curios why default sling servlet was not working?

maybe I have to change some something in system console?