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

Creating your first servlet in AEM Forms | AEM Community Blog Seeding

Avatar

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,157 likes

Total Posts

6,247 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile

Avatar
Establish
Community Manager
kautuk_sahni
Community Manager

Likes

1,157 likes

Total Posts

6,247 posts

Correct Reply

1,144 solutions
Top badges earned
Establish
Coach
Originator
Contributor 2
Contributor
View profile
kautuk_sahni
Community Manager

03-06-2021

BlogImage.jpg

Creating your first servlet in AEM Forms by Adobe Docs

Abstract

Sling Servlet
A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet technology defines HTTP-specific servlet classes.
All servlets must implement the Servlet interface, which defines life-cycle methods.

A servlet in AEM can be registered as OSGi service: you can extend SlingSafeMethodsServlet for read-only implementation or SlingAllMethodsServlet in order to implement all RESTful operations.

Servlet Code
import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.FormsService;

@Component(service={Servlet.class}, property={"sling.servlet.methods=post", "sling.servlet.paths=/bin/mergedataWithAcroform"})
public class MyFirstAEMFormsServlet extends SlingAllMethodsServlet
{

private static final long serialVersionUID = 1L;
@Reference
FormsService formsService;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
{
String file_path = request.getParameter("save_location");

java.io.InputStream pdf_document_is = null;
java.io.InputStream xml_is = null;
javax.servlet.http.Part pdf_document_part = null;
javax.servlet.http.Part xml_data_part = null;
try
{
pdf_document_part = request.getPart("pdf_file");
xml_data_part = request.getPart("xml_data_file");
pdf_document_is = pdf_document_part.getInputStream();
xml_is = xml_data_part.getInputStream();
Document data_merged_document = formsService.importData(new Document(pdf_document_is), new Document(xml_is));
data_merged_document.copyToFile(new File(file_path));

}
catch(Exception e)
{
response.sendError(400,e.getMessage());
}
}
}Servlet Code
import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.api.FormsService;

@Component(service={Servlet.class}, property={"sling.servlet.methods=post", "sling.servlet.paths=/bin/mergedataWithAcroform"})
public class MyFirstAEMFormsServlet extends SlingAllMethodsServlet
{

private static final long serialVersionUID = 1L;
@Reference
FormsService formsService;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
{
String file_path = request.getParameter("save_location");

java.io.InputStream pdf_document_is = null;
java.io.InputStream xml_is = null;
javax.servlet.http.Part pdf_document_part = null;
javax.servlet.http.Part xml_data_part = null;
try
{
pdf_document_part = request.getPart("pdf_file");
xml_data_part = request.getPart("xml_data_file");
pdf_document_is = pdf_document_part.getInputStream();
xml_is = xml_data_part.getInputStream();
Document data_merged_document = formsService.importData(new Document(pdf_document_is), new Document(xml_is));
data_merged_document.copyToFile(new File(file_path));

}
catch(Exception e)
{
response.sendError(400,e.getMessage());
}
}
}

Read Full Blog

Creating your first servlet in AEM Forms

Q&A

Please use this thread to ask the related questions.

AEM AEMIBlogSeeding Experience Manager