How to define a servlet path using the new servlet annotations? | Community
Skip to main content
Level 8
Apr 20, 2021 7:50 PM UTC
Solved

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

The new way to annotate servlets is like this:

 

@8220494(service = { Servlet.class }) @SlingServletResourceTypes( resourceTypes="/apps/my/type", methods= "GET", extensions="html", selectors="hello") public class MyServlet extends SlingSafeMethodsServlet { @9944223 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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
TB3dockAccepted solution
Level 8
Apr 20, 2021 9:31 PM UTC

Ok, found the solution:

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

2 replies

TB3dockAuthorAccepted solution
Level 8
Apr 20, 2021 9:31 PM UTC

Ok, found the solution:

@SlingServletPaths(value="/bin/somepath") private class MyServlet extends SlingAllMethodsServlet
Translate
Ravi_Pampana
New Participant
Apr 20, 2021 8:36 PM UTC

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

 

@8220494(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

 

Hope this helps

 

Translate