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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Page property in servlet

Avatar

Level 5

I have a servlet which provides JSON for the page. I wanted to get page properties in my code in that servlet. But every time I add page manager or current page to my servlet, the JSON stops loading. And there's no error.

I have the page path. But adapting it to a page is causing issue.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can refer below code , I am accessing servlet via resourceType-

 


package com.aem.demo.core.servlets;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;

@component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "demo/components/page",
"sling.servlet.methods=" + HttpConstants.METHOD_GET })
public class ReadPageInformationServlet extends SlingAllMethodsServlet {

private final Logger logger = LoggerFactory.getLogger(ReadPageInformationServlet.class);

/**
*
*/
private static final long serialVersionUID = 1L;

protected void doGet(SlingHttpServletRequest slingHttpServletRequest,
SlingHttpServletResponse slingHttpServletResponse) {

String requestURI = slingHttpServletRequest.getRequestURI();

String currentpagePath = requestURI.substring(0, requestURI.lastIndexOf("/"));

ResourceResolver resourceResolver = slingHttpServletRequest.getResourceResolver();

Resource resource = resourceResolver.getResource(currentpagePath);
if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

Page page = resource.adaptTo(Page.class);
ValueMap pageproperties = page.getProperties();
try {
slingHttpServletResponse.getWriter().println(pageproperties);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}

View solution in original post

0 Replies

Avatar

Level 3

Hi ,

 

 

Read the value in servlet by passing the current page pat in ajax-data.

 

where you consume the servlet - data - URI = ${currentPage.path @CONTEXT= ""}.

 

Read the URI value in servlet - request.getParameter("URI");

 

Thanks,

Nani

Avatar

Community Advisor

Hi @Shaheena_Sheikh, I am not sure how you are trying to retrieve page properties, however you can try below code. It assumes that resource resolver from request will have read permissions to the page which page properties you would like to get.

 

ResourceResolver resourceResolver = request.getResourceResolver();
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(pagePath); 
ValueMap pageProperties = page.getProperties();

 

Avatar

Correct answer by
Community Advisor

You can refer below code , I am accessing servlet via resourceType-

 


package com.aem.demo.core.servlets;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.cq.wcm.api.Page;

@component(service = Servlet.class, property = { "sling.servlet.resourceTypes=" + "demo/components/page",
"sling.servlet.methods=" + HttpConstants.METHOD_GET })
public class ReadPageInformationServlet extends SlingAllMethodsServlet {

private final Logger logger = LoggerFactory.getLogger(ReadPageInformationServlet.class);

/**
*
*/
private static final long serialVersionUID = 1L;

protected void doGet(SlingHttpServletRequest slingHttpServletRequest,
SlingHttpServletResponse slingHttpServletResponse) {

String requestURI = slingHttpServletRequest.getRequestURI();

String currentpagePath = requestURI.substring(0, requestURI.lastIndexOf("/"));

ResourceResolver resourceResolver = slingHttpServletRequest.getResourceResolver();

Resource resource = resourceResolver.getResource(currentpagePath);
if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

Page page = resource.adaptTo(Page.class);
ValueMap pageproperties = page.getProperties();
try {
slingHttpServletResponse.getWriter().println(pageproperties);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}