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

SOLVED

Sling servlets doesn't work for txt extension

pradeepd1320668
Level 2
Level 2

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

1 Accepted Solution
Prince_Shivhare
Correct answer by
Level 9
Level 9

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 {

View solution in original post

8 Replies
asutosh_jena
Community Advisor
Community Advisor

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

asutosh_jena
Community Advisor
Community Advisor

Hi @pradeepd1320668 

 

OSGi DS 1.4 (R7) component property type annotations for Sling Servlets uses @SlingServletResourceTypes

 

Need to add the below dependency:

<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.servlets.annotations</artifactId>
<version>1.2.4</version>
</dependency>

Please see link below:

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

 

Thanks

pradeepd1320668
Level 2
Level 2
None of those are working in txt extension. 2nd and 3rd logic is not working for xml extension also. First : @SlingServlet(resourceTypes = "/apps/test/components/page/blankpage", selectors = "301", extensions = "xml", methods = "GET") Second : @component(service = Servlet.class, property = {"process.label=Some Servlet", Constants.SERVICE_DESCRIPTION + "=Some Servlet"}) @SlingServletResourceTypes(resourceTypes = "/apps/test/components/page/blankpage", methods = HttpConstants.METHOD_GET, extensions = "xml", selectors = "301") Third : @component(service = Servlet.class, property = { "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.resourceTypes=" + "/apps/test/components/page/blankpage", "sling.servlet.selectors=301", "sling.servlet.extensions=xml", "service.ranking:Integer=2" })
KiranVedantam1992
Community Advisor
Community Advisor

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

Prince_Shivhare
Correct answer by
Level 9
Level 9

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 {

View solution in original post

BrianKasingli
Community Advisor
Community Advisor

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:

 

pradeepd1320668
Level 2
Level 2

I could not find @SlingServletResourceTypes annotation. Can you please share complete servlet code?