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

Sling servlets doesn't work for txt extension

Avatar

Avatar
Validate 1
Level 1
pradeepd1320668
Level 1

Likes

2 likes

Total Posts

30 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 1
View profile

Avatar
Validate 1
Level 1
pradeepd1320668
Level 1

Likes

2 likes

Total Posts

30 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 1
View profile
pradeepd1320668
Level 1

07-05-2021

Hi All,

 

I am using felix scr annotations. 

Sling servlet working fine for xml extension but not for txt extension.

 

Attaching servlet code for reference

@service()
@SlingServlet(resourceTypes = "/apps/test/components/page/blankpage",
selectors = "301",
extensions = "txt",
methods = "GET",
metatype =true)
public class TestServlet extends SlingSafeMethodsServlet {

/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LoggerFactory.getLogger(TestServlet.class);

@Override
protected void doGet(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
LOGGER.info("Entering doGet");
resp.setContentType("text/html");
resp.getWriter().println(response);

}
}

 

Any pointers is highly appreciated.

Gone through below link, not helped anymore.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/sling-servlet-txt-extensio...

 

 

Thanks,

Pradeep

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
Level 7
Prince_Shivhare
Level 7

Likes

107 likes

Total Posts

393 posts

Correct Reply

63 solutions
Top badges earned
Springboard
Establish
Give Back 100
Boost 100
Validate 1
View profile

Avatar
Springboard
Level 7
Prince_Shivhare
Level 7

Likes

107 likes

Total Posts

393 posts

Correct Reply

63 solutions
Top badges earned
Springboard
Establish
Give Back 100
Boost 100
Validate 1
View profile
Prince_Shivhare
Level 7

07-05-2021

my suggestion would be not to use the SCR annotation becasue it is depricated now.

Please use the OSGI annotations.

org.osgi.service.component.annotations.Component;

@Component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + "=JSON Servlet to read the data from the external webservice",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/SimpleServlet"
})
public class SimpleServlet extends SlingSafeMethodsServlet {

Answers (3)

Answers (3)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

611 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

07-05-2021

Here's an example of working code with

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

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

txt:

 

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

169 likes

Total Posts

180 posts

Correct Reply

54 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

169 likes

Total Posts

180 posts

Correct Reply

54 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

07-05-2021

Hi @pradeepd1320668,

 

Can you please use the latest OSGI declarative services annotations [R7]  annotations like this:

@Component(service = Servlet.class,
property = {
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.resourceTypes=" + ADD THE RESOURCE TYPE,
"sling.servlet.selectors=ADD SELECTORS",
"sling.servlet.extensions=txt",
"service.ranking:Integer=ADD THE RANKING"
})

 

Hope this helps!

 

Thanks,

Kiran Vedantam

Avatar

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

551 likes

Total Posts

663 posts

Correct Reply

191 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile
asutosh_jena
Level 10

07-05-2021

Hi @pradeepd1320668 

 

Please use the below code:

@Component(service = Servlet.class, property = {"process.label=Some Servlet",
Constants.SERVICE_DESCRIPTION + "=Some Servlet"})
@@SlingServletResourceType(resourceTypes = "test/components/page/blankpage",
methods = HttpConstants.METHOD_GET,
extensions = "txt",
selectors = "something")
public class TestServlet extends SlingSafeMethodsServlet {
//Rest of code
}

Here is a detailed exampale with video:

http://www.sgaemsolutions.com/2017/12/apache-sling-servlets-and-scripts.html

 

Make sure the resource you are accessing has the sling:resourceType as "test/components/page/blankpage".

 

Let's say you are accessing /content/mywebsite/abc/jcr:content.something.txt then abc page jcr:content should have the above property as sling:resourceType

Hope this will help!

Thanks