OpenAI ChatGPT vs Google Bard for Devlopment Support
Albin Issac
DXP Expert | Martech Enthusiast | Generative AI Enthusiast | Adobe Community Advisor | Tech Blogger | Digital Architect at Boston Scientific
I was testing the OpenAI ChatGPT and Google Bard to see how both can help the AEM developers to simplify their development efforts. Here is the result.
ChatGPT - New chat (openai.com):
Question 1: Create an AEM servlet.
Here is the response with a complete working servlet.
package com.example.aem.servlets
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;
@Component(service = Servlet.class, property = { "sling.servlet.paths=/bin/example", "sling.servlet.methods=POST" })
public class ExampleServlet extends SlingAllMethodsServlet {
? private static final long serialVersionUID = 1L;
? @Override
? protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
? ? // Add your servlet logic here
? }
}
Question 2: Write a logic to export all the tag details
Complete code - this time, SCR annotation is used(deprecated for the latest AEM versions); the servlet name and details are changed to match the purpose.
package com.example.aem.servlets
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Export Tag Details Servlet",
? ? ? ? "sling.servlet.paths=/bin/exportTagDetails", "sling.servlet.methods=GET" })
@Properties({ @Property(name = "service.description", value = "Export Tag Details Servlet"),
? ? ? ? @Property(name = "service.vendor", value = "Adobe"),
? ? ? ? @Property(name = "sling.servlet.paths", value = "/bin/exportTagDetails") })
@Service
public class ExportTagDetailsServlet extends SlingAllMethodsServlet {
? ? private static final long serialVersionUID = 1L;
? ? private static final Logger LOG = LoggerFactory.getLogger(ExportTagDetailsServlet.class);
? ? protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
? ? ? ? ? ? throws ServletException, IOException {
? ? ? ? Session session = null;
? ? ? ? try {
? ? ? ? ? ? response.setContentType("text/csv");
? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=\"tagDetails.csv\"");
? ? ? ? ? ? session = request.getResourceResolver().adaptTo(Session.class);
? ? ? ? ? ? Node root = session.getNode("/etc/tags");
? ? ? ? ? ? List<String[]> rows = new ArrayList<>();
? ? ? ? ? ? String[] header = { "Tag Name", "Tag Title", "Tag Description", "Tag Path", "Tag Owner", "Tag Created Date" };
? ? ? ? ? ? rows.add(header);
? ? ? ? ? ? NodeIterator nodeItr = root.getNodes();
? ? ? ? ? ? while (nodeItr.hasNext()) {
? ? ? ? ? ? ? ? Node tagNode = nodeItr.nextNode();
? ? ? ? ? ? ? ? String[] row = new String[6];
? ? ? ? ? ? ? ? row[0] = tagNode.getName();
? ? ? ? ? ? ? ? row[1] = tagNode.getProperty("jcr:title").getString();
? ? ? ? ? ? ? ? row[2] = tagNode.getProperty("jcr:description").getString();
? ? ? ? ? ? ? ? row[3] = tagNode.getPath();
? ? ? ? ? ? ? ? row[4] = tagNode.getProperty("tagOwner").getString();
? ? ? ? ? ? ? ? row[5] = tagNode.getProperty("jcr:created").getValue().getString();
? ? ? ? ? ? ? ? rows.add(row);
? ? ? ? ? ? }
? ? ? ? ? ? StringBuilder csv = new StringBuilder();
? ? ? ? ? ? Iterator<String[]> rowItr = rows.iterator();
? ? ? ? ? ? while (rowItr.hasNext()) {
? ? ? ? ? ? ? ? String[] row = rowItr.next();
? ? ? ? ? ? ? ? for (int i = 0; i < row.length; i++) {
? ? ? ? ? ? ? ? ? ? csv.append(row[i]).append(",");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? csv.append("\n");
? ? ? ? ? ? }
? ? ? ? ? ? response.getWriter().write(csv.toString());
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? LOG.error("Error exporting tag details", e);
? ? ? ? } finally {
? ? ? ? ? ? if (session != null) {
? ? ? ? ? ? ? ? session.logout();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
Question 3: Restrict the tags export to the test folder
Responded with modified doGet method that exports only the tags under test folders.
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response
? ? throws ServletException, IOException {
? ? Session session = null;
? ? try {
? ? ? ? response.setContentType("text/csv");
? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=\"tagDetails.csv\"");
? ? ? ? session = request.getResourceResolver().adaptTo(Session.class);
? ? ? ? Node root = session.getNode("/etc/tags");
? ? ? ? List<String[]> rows = new ArrayList<>();
? ? ? ? String[] header = { "Tag Name", "Tag Title", "Tag Description", "Tag Path", "Tag Owner", "Tag Created Date" };
? ? ? ? rows.add(header);
? ? ? ? NodeIterator nodeItr = root.getNodes();
? ? ? ? while (nodeItr.hasNext()) {
? ? ? ? ? ? Node tagNode = nodeItr.nextNode();
? ? ? ? ? ? if (tagNode.getPath().startsWith("/etc/tags/test")) {
? ? ? ? ? ? ? ? String[] row = new String[6];
? ? ? ? ? ? ? ? row[0] = tagNode.getName();
? ? ? ? ? ? ? ? row[1] = tagNode.getProperty("jcr:title").getString();
? ? ? ? ? ? ? ? row[2] = tagNode.getProperty("jcr:description").getString();
? ? ? ? ? ? ? ? row[3] = tagNode.getPath();
? ? ? ? ? ? ? ? row[4] = tagNode.getProperty("tagOwner").getString();
? ? ? ? ? ? ? ? row[5] = tagNode.getProperty("jcr:created").getValue().getString();
? ? ? ? ? ? ? ? rows.add(row);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? StringBuilder csv = new StringBuilder();
? ? ? ? Iterator<String[]> rowItr = rows.iterator();
? ? ? ? while (rowItr.hasNext()) {
? ? ? ? ? ? String[] row = rowItr.next();
? ? ? ? ? ? for (int i = 0; i < row.length; i++) {
? ? ? ? ? ? ? ? csv.append(row[i]).append(",");
? ? ? ? ? ? }
? ? ? ? ? ? csv.append("\n");
? ? ? ? }
? ? ? ? response.getWriter().write(csv.toString());
? ? } catch (Exception e) {
? ? ? ? LOG.error("Error exporting tag details", e);
? ? } finally {
? ? ? ? if (session != null) {
? ? ? ? ? ? session.logout();
? ? ? ? }
? ? }
}
)
Question 4:Rewrite the servlet using OSGI 6 annotation
The deprecated SCR annotations are replaced with OSGI 6/7 annotations.
Final code - the imports were missing, but it used the earlier logic and restriction
@Component(service = Servlet.class, property = { "sling.servlet.paths=/bin/exporttags" }
public class ExportTagsServlet extends SlingAllMethodsServlet {
? ? private static final Logger LOG = LoggerFactory.getLogger(ExportTagsServlet.class);
? ? @Override
? ? protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
? ? ? ? ? ? throws ServletException, IOException {
? ? ? ? Session session = null;
? ? ? ? try {
? ? ? ? ? ? response.setContentType("text/csv");
? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=\"tagDetails.csv\"");
? ? ? ? ? ? session = request.getResourceResolver().adaptTo(Session.class);
? ? ? ? ? ? Node root = session.getNode("/etc/tags");
? ? ? ? ? ? List<String[]> rows = new ArrayList<>();
? ? ? ? ? ? String[] header = { "Tag Name", "Tag Title", "Tag Description", "Tag Path", "Tag Owner", "Tag Created Date" };
? ? ? ? ? ? rows.add(header);
? ? ? ? ? ? NodeIterator nodeItr = root.getNodes();
? ? ? ? ? ? while (nodeItr.hasNext()) {
? ? ? ? ? ? ? ? Node tagNode = nodeItr.nextNode();
? ? ? ? ? ? ? ? if (tagNode.getPath().startsWith("/etc/tags/test")) {
? ? ? ? ? ? ? ? ? ? String[] row = new String[6];
? ? ? ? ? ? ? ? ? ? row[0] = tagNode.getName();
? ? ? ? ? ? ? ? ? ? row[1] = tagNode.getProperty("jcr:title").getString();
? ? ? ? ? ? ? ? ? ? row[2] = tagNode.getProperty("jcr:description").getString();
? ? ? ? ? ? ? ? ? ? row[3] = tagNode.getPath();
? ? ? ? ? ? ? ? ? ? row[4] = tagNode.getProperty("tagOwner").getString();
? ? ? ? ? ? ? ? ? ? row[5] = tagNode.getProperty("jcr:created").getValue().getString();
? ? ? ? ? ? ? ? ? ? rows.add(row);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? StringBuilder csv = new StringBuilder();
? ? ? ? ? ? Iterator<String[]> rowItr = rows.iterator();
? ? ? ? ? ? while (rowItr.hasNext()) {
? ? ? ? ? ? ? ? String[] row = rowItr.next();
? ? ? ? ? ? ? ? for (int i = 0; i < row.length; i++) {
? ? ? ? ? ? ? ? ? ? csv.append(row[i]).append(",");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? csv.append("\n");
? ? ? ? ? ? }
? ? ? ? ? ? response.getWriter().write(csv.toString());
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? LOG.error("Error exporting tag details", e);
? ? ? ? } finally {
? ? ? ? ? ? if (session != null) {
? ? ? ? ? ? ? ? session.logout();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
)
Question 5: Include the imports
Imports were listed - one of the imports was missing, but the code was compiled without issue after adding the missing import.
import java.io.IOException
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.servlet.ServletException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
领英推荐
Google Bard - Bard (google.com):
Question 1: Create an AEM servlet.
The servlet created with SCR annotation(deprecated for latest AEM versions)
package com.example.myservlet
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingServlet;
@SlingServlet(paths = "/content/my-servlet")
public class MyServlet implements SlingServlet {
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("text/html");
response.getWriter().print("<h1>Hello, world!</h1>");
}
@Override
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
// Process the POST request here
}
};
Question 2: Update the servlet to use OSGI 6 annotation.
I received an updated servlet but not compilable
package com.example.myservlet
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingServlet;
import org.osgi.service.component.annotations.Component;
@Component(service = SlingHttpServlet.class, immediate = true)
public class MyServlet implements SlingHttpServlet {
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("text/html");
response.getWriter().print("<h1>Hello, world!</h1>");
}
@Override
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
// Process the POST request here
}
};
Question 3: Write a logic to export all the tag details
I received the below logic; somehow, the previous conversation was not remembered.
public void exportTags()
// Get the list of all tags
List<Tag> tags = TagManager.getTags();
// Create a CSV writer
CSVWriter writer = new CSVWriter(new FileWriter("tags.csv"));
// Write the header row
writer.writeNext("Name", "Description");
// Write each tag
for (Tag tag : tags) {
writer.writeNext(tag.getName(), tag.getDescription());
}
// Close the writer
writer.close();
}{
Question 4: Restrict the tags export to the test folder
Received the updated logic
public void exportTags()
// Get the list of all tags
List<Tag> tags = TagManager.getTags();
// Create a CSV writer
CSVWriter writer = new CSVWriter(new FileWriter("tags.csv"));
// Write the header row
writer.writeNext("Name", "Description");
// Write each tag
for (Tag tag : tags) {
if (tag.getFolder().getPath().startsWith("/test")) {
writer.writeNext(tag.getName(), tag.getDescription());
}
}
// Close the writer
writer.close();
}{
Question 5: Please give me the final servlet
Here is the final servlet - the logic and the restrictions are enabled but have compilation errors.
import org.apache.sling.api.SlingHttpServletRequest
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingServlet;
import org.osgi.service.component.annotations.Component;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.TagManager;
@Component(service = SlingHttpServlet.class, immediate = true)
public class MyServlet implements SlingHttpServlet {
@Override
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.getWriter().print("<h1>Hello, world!</h1>");
// Export all tags in the test folder
exportTags();
}
@Override
public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
// Process the POST request here
}
private void exportTags() {
// Get the list of all tags
List<Tag> tags = TagManager.getTags();
// Create a CSV writer
CSVWriter writer = new CSVWriter(new FileWriter("tags.csv"));
// Write the header row
writer.writeNext("Name", "Description");
// Write each tag
for (Tag tag : tags) {
if (tag.getFolder().getPath().startsWith("/test")) {
writer.writeNext(tag.getName(), tag.getDescription());
}
}
// Close the writer
writer.close();
}
};
ChatGPT provides a better code that remembers previous conversations and outputs the code based on earlier conversations and context. The code generated by Google Bard is not completely compilable also; it needed to include the earlier context while outputting the code.
It looks to be Google Bard is not yet ready for coding, but ChatGPT is ahead of that we can expect improvements to the Google Bard shortly.
In the current state of AI, the developer needs to have an understanding of the latest features of frameworks and languages to develop optimal code(the language models may not have visibility to the latest updates, or they may generate the code based on the old approach; the developer should know the latest updates)
The AI models can help the developers avoid spending time writing the code from scratch, but they can enable the code developer to modify it.
Technology leader, Architect (Adobe) at Discover Financial Services, Adobe Community Advisor/Contributor
1 年Pretty good!