Default Sling Servlet | Community
Skip to main content
Xena_bcn
New Participant
October 16, 2015
Solved

Default Sling Servlet

  • October 16, 2015
  • 17 replies
  • 6070 views

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 ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by edubey

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-services

17 replies

Xena_bcn
Xena_bcnAuthor
New Participant
October 16, 2015

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.

edubey
New Participant
October 16, 2015

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

Xena_bcn
Xena_bcnAuthor
New Participant
October 16, 2015

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

smacdonald2008
New Participant
October 16, 2015

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. 

edubey
New Participant
October 16, 2015

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.

Xena_bcn
Xena_bcnAuthor
New Participant
October 16, 2015

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.

Xena_bcn
Xena_bcnAuthor
New Participant
October 16, 2015

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?