Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

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

Avatar

Level 5

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.

Tessa_learner1_0-1648193763940.pngTessa_learner1_1-1648193799113.png

 

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();
}
}
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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-submis...

[2] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-form-should-send-paylo...

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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-submis...

[2] - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/aem-form-should-send-paylo...

Avatar

Community Advisor

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