I have created a servlet and have implemented the http connection using HttpsURLConnection but unable to connect the same to post.Post.jsp. I had created an object of that and tried implementing the function but can find not hits to the url in browser network.Kindly share an example or Sample example to connect to 3rd party REST API in AEM on custom submit action / on button click as developer ?
Solved! Go to Solution.
Views
Replies
Total Likes
You will need to write a custom submit action as explained in the article that I sent you. In the custom submit post.jsp you will add the following code
com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,"/bin/storeafsubmission",null,null);
Repalce the /bin/storeafsubmission with your path(/bin/demo/httpcall)
your servlet is expecting a GET call. You will need to change it to make it to POST servlet. For that you need to extend SlingAllMethodsServlet
so you have created a servlet and want to call the servlet on form submission?
yes exactly ...
HttpServlet.java
As I am a beginner kindly let me know if there is any other possibility of doing rest api call directly in post.Post.jsp
your servlet is expecting a GET call. You will need to change it to make it to POST servlet. For that you need to extend SlingAllMethodsServlet
This is one way of doing it
or you can do it this way
In the above example, it is submitting to external end point, but you can submit to your servlet running in AEM
You will need to write a custom submit action as explained in the article that I sent you. In the custom submit post.jsp you will add the following code
com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,"/bin/storeafsubmission",null,null);
Repalce the /bin/storeafsubmission with your path(/bin/demo/httpcall)
Appreciate your quick reply !!!..
Will try out the same
Add below code in post.POST.jsp file while creating custom submit action.
"/bin/storeafsubmission" this is my servlet path.
This code works for me in aem 6.4 version
<%@include file="/libs/fd/af/components/guidesglobal.jsp"%>
<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.api.request.RequestParameter,
com.day.cq.wcm.api.WCMMode,
com.day.cq.wcm.foundation.forms.FormsHelper,
org.apache.sling.api.resource.ResourceUtil,
org.apache.sling.api.resource.ValueMap,
com.adobe.forms.common.submitutils.CustomParameterRequest,
com.adobe.aemds.guide.utils.*" %>
<%@ page import="org.apache.sling.api.request.RequestParameter,
com.day.cq.wcm.api.WCMMode" %>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0"%>
<cq:defineObjects/>
<sling:defineObjects/>
<%@page session="false" %>
<%
com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,"/bin/storeafsubmission",null,null);
%>
Links to create custom submit actions:
package com.aem.core.servlets; import com.aem.core.utils.Constants; import com.day.cq.wcm.api.Page; import com.day.cq.wcm.api.PageManager; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.request.RequestParameter; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.osgi.service.component.annotations.Component; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.Servlet; import javax.servlet.ServletException; import java.io.IOException; import java.util.Arrays; import java.util.Iterator; import java.util.List; import static org.apache.sling.api.servlets.ServletResolverConstants.*; @Component( service= {Servlet.class}, property={ "sling.servlet.methods="+ HttpConstants.METHOD_GET, SLING_SERVLET_METHODS+"="+HttpConstants.METHOD_POST, "sling.servlet.resourceTypes="+ "aem/components/structure/test-home", SLING_SERVLET_PATHS+"="+"/test/r5servlet", "sling.servlet.selectors=" + "test", "sling.servlet.selectors=" + "ds", SLING_SERVLET_EXTENSIONS+"="+"xml", "sling.servlet.extensions"+"="+"txt" }) public class Test extends SlingAllMethodsServlet { @Override protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException { final ResourceResolver resourceResolver = req.getResourceResolver(); String reqSelectors="SELECTORS => "; String reqExtension=req.getRequestPathInfo().getExtension(); try { String[] selectors=req.getRequestPathInfo().getSelectors(); for(String selector : selectors){ reqSelectors=reqSelectors +" "+selector; } } catch (Exception e) { LOG.info("\n ERROR {} ", e.getMessage()); } resp.setContentType("application/json"); resp.getWriter().write(reqSelectors+ " # " +reqExtension); } @Override protected void doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws ServletException, IOException { try { LOG.info("\n ------------------------STARTED POST-------------------------"); List<RequestParameter> requestParameterList=req.getRequestParameterList(); for(RequestParameter requestParameter : requestParameterList){ LOG.info("\n ==PARAMETERS===> {} : {} ",requestParameter.getName(),requestParameter.getString()); } }catch (Exception e){ LOG.info("\n ERROR IN REQUEST {} ",e.getMessage()); } resp.getWriter().write("======FORM SUBMITTED========"); } }
@JADISH if you are beginner I would say rather than hit and trial go with the basic on how sling servlets resolves.
start from here
https://www.youtube.com/watch?v=ZAlR0nPdpSI
Here is a working java file for servlet:
Link to git: https://github.com/aemgeeks1212/aemgeeks
Views
Likes
Replies
Views
Likes
Replies