How to call Servlet/Sling model for Custom Action type in Forms | Community
Skip to main content
Level 4
March 25, 2022
Solved

How to call Servlet/Sling model for Custom Action type in Forms

  • March 25, 2022
  • 2 replies
  • 2189 views

Hi, 

I have created custom form action to invoke third party API after submitting the AEM form container. Now I want to call the Servlet which will have code for the api call. When I select custom action in the form container it asking for the id. Plese help.

 

and here is my sevlet code

package com.adobe.aem.FormsDemo.core.servlets;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=JSON path based servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/postservletexample" })public class CustomServlet {
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
try {
URL url = new URL("https://mocki.io/v1/8a06b1d3-cda3-4e9e-a767-fdc662311718");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
int status = connection.getResponseCode();
System.out.println(status);
if (status!=0){
response.getWriter().write("Success");
}else{
response.getWriter().write("Failed");
}
// response.setStatus(SlingHttpServletResponse.SC_OK);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by lukasz-m

Hi @tessa_learner1, the ID field is use to set id attribute in html form element.

In general if you are using Action Type then you will not be able to use custom servlet. If you want to use your custom servlet with Form Container you will need to do some changes in Form Container component itself, as it does not give any configuration option to change servlet that handles it.

Please have a look into accepted message in this thread - [1] - it describes what needs to be done to use custom servlet with Form Container component - it's all there including sample code.

Nevertheless if you want to use custom servlet instead the one provided by Form Container, maybe it will be better to create your own custom form component - I have described few approaches in this thread - [2]

 

[1] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/form-core-component-submission-using-custom-action-type/m-p/438328/highlight/true#M125160

[2] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-form-should-send-payload-to-an-external-api-to-show-success/m-p/445641/highlight/true#M127423

2 replies

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
March 25, 2022

Hi @tessa_learner1, the ID field is use to set id attribute in html form element.

In general if you are using Action Type then you will not be able to use custom servlet. If you want to use your custom servlet with Form Container you will need to do some changes in Form Container component itself, as it does not give any configuration option to change servlet that handles it.

Please have a look into accepted message in this thread - [1] - it describes what needs to be done to use custom servlet with Form Container component - it's all there including sample code.

Nevertheless if you want to use custom servlet instead the one provided by Form Container, maybe it will be better to create your own custom form component - I have described few approaches in this thread - [2]

 

[1] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/form-core-component-submission-using-custom-action-type/m-p/438328/highlight/true#M125160

[2] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-form-should-send-payload-to-an-external-api-to-show-success/m-p/445641/highlight/true#M127423

Sanjay_Bangar
Community Advisor
Community Advisor
March 25, 2022

Hi @tessa_learner1 ,

You need create sling Servlet using resource type and add  resource type to your form container component drop-down.then your Servlet getting called.

Kr,

Sanjay