Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to define a servlet path using the new servlet annotations?

Avatar

Avatar
Ignite 10
Level 4
TB3dock
Level 4

Likes

34 likes

Total Posts

203 posts

Correct Reply

4 solutions
Top badges earned
Ignite 10
Boost 25
Give Back 25
Validate 10
Validate 1
View profile

Avatar
Ignite 10
Level 4
TB3dock
Level 4

Likes

34 likes

Total Posts

203 posts

Correct Reply

4 solutions
Top badges earned
Ignite 10
Boost 25
Give Back 25
Validate 10
Validate 1
View profile
TB3dock
Level 4

20-04-2021

The new way to annotate servlets is like this:

 

@component(service = { Servlet.class })
@SlingServletResourceTypes(
    resourceTypes="/apps/my/type", 
    methods= "GET",
    extensions="html",
    selectors="hello")
public class MyServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        ...
    }
}

 

 

How can one register by path, not by resource type?

 

The documenation:

 

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

 

Has no info on this,it mentions "sling.servlet.paths" but his is the old way, not the new way?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Ignite 10
Level 4
TB3dock
Level 4

Likes

34 likes

Total Posts

203 posts

Correct Reply

4 solutions
Top badges earned
Ignite 10
Boost 25
Give Back 25
Validate 10
Validate 1
View profile

Avatar
Ignite 10
Level 4
TB3dock
Level 4

Likes

34 likes

Total Posts

203 posts

Correct Reply

4 solutions
Top badges earned
Ignite 10
Boost 25
Give Back 25
Validate 10
Validate 1
View profile
TB3dock
Level 4

20-04-2021

Ok, found the solution:

@SlingServletPaths(value="/bin/somepath")
private class MyServlet extends SlingAllMethodsServlet

Answers (1)

Answers (1)

Avatar

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

190 likes

Total Posts

239 posts

Correct Reply

74 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

190 likes

Total Posts

239 posts

Correct Reply

74 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile
Ravi_Pampana
MVP

20-04-2021

Hi,

 

You can use below syntax for registering the servlet with paths

 

@Component(service = Servlet.class,

        property = {

                Constants.SERVICE_DESCRIPTION + "=Some Description",

                "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                "sling.servlet.paths=" + "/bin/somepath",

                "sling.servlet.extensions=json" 

        })

 

You can use the resourceType also to bind the servlet to a path

 

@component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Some Description",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.resourceTypes=apps/servlets/sometext" })

 

Create a node under content folder and add the resourceType as given in the servlet and hitting the page url will trigger the servlet.

 

Url to trigger the servlet: http://localhost:4502/content/wknd-events/servlet_nodes/servlets/newservlet

 

Screen Shot 2021-04-20 at 1.35.40 PM.png

Hope this helps