AEM 6.2 invoke JSP code after HTTP POST request | Community
Skip to main content
New Participant
August 10, 2016
Solved

AEM 6.2 invoke JSP code after HTTP POST request

  • August 10, 2016
  • 11 replies
  • 5952 views

Hi All,

Is it possible to invoke JSP code in AEM 6.2 following an HTTP POST request?

I have managed to get it working for HTTP GET but I can’t get it working for HTTP POST.

I have listed below the steps I have gone through so far to get JSP code running with HTTP GET and to try to get it working with HTTP POST.

Thank you for you help,
Best regards,
Robert

————

Here are the steps I have gone through so far :-

1. In browser, navigate to http://localhost:4502/crx/de/index.jsp#/apps
2. Create a Component called “simpleCounterIncrementer”
3. Paste the JSP code below into “simpleCounterIncrementer.jsp”

    <%@ page import = "javax.jcr.*,org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.resource.ResourceResolver,org.apache.sling.api.resource.Resource" %>
    <%
    final String simpleCounterNode = "simpleCounterNode";

    final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
    final ResourceResolver resourceResolver = slingRequest.getResourceResolver();
    final Resource resource = resourceResolver.getResource("/content");
    Node node = resource.adaptTo(Node.class);
    if (node.hasNode(simpleCounterNode))
    {
        node = node.getNode(simpleCounterNode);
    }
    else
    {
        node.addNode(simpleCounterNode);
        node.save();
        node = node.getNode(simpleCounterNode);
    }

    final String simpleCounterProperty = "simpleCounterProperty";

    if (!node.hasProperty(simpleCounterProperty))
    {
        node.setProperty(simpleCounterProperty, 0);
    }
    final Property counterProperty = node.getProperty(simpleCounterProperty);
    counterProperty.setValue(counterProperty.getLong() + 1);
    counterProperty.save();
    %>

4. In browser, navigate to http://localhost:4502/crx/de/index.jsp#/content
5. Create a Node called “simpleCounterIncrementer” and set “jcr:primaryType” = “cq:Page”
6. Create a sub-Node called “jcr:content” and set “jcr:primaryType” = “cq:PageContent” and set “sling:resourceType” = “simpleCounterIncrementer”

7. Open a terminal window and run the command below FIVE times

    curl --user admin:admin -X GET http://localhost:4502/content/simpleCounterIncrementer.html

8. In browser, navigate to http://localhost:4502/crx/de/index.jsp#/content/simpleCounterNode and observe "simpleCounterProperty" in 5 because HTTP GET has run five times

9. In browser, navigate to http://localhost:4502/crx/de/index.jsp#/apps/simpleCounterIncrementer

10. Copy “simpleCounterIncrementer.jsp”  and paste it as “simpleCounterIncrementer.POST.jsp”

11. Open a terminal window and run the command below

      curl --user admin:admin -X POST http://localhost:4502/content/simpleCounterIncrementer.html

12. In browser, navigate to http://localhost:4502/crx/de/index.jsp#/content/simpleCounterNode and observe "simpleCounterProperty" in still 5 even though HTTP POST has run


Please can you advise if it is possible to invoke JSP code in AEM 6.2 following an HTTP POST request?

Please can you advise what additional steps I need?

Thank you for your time,
Best regards,
Robert

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 RobertBailey1

Thank you for your responses.  For the benefit of anyone else who wants a "simple" set of instructions how to create a POST request handler for AEM 6.2, here goes :-

1.  Install Maven and install Eclipse IDE for Java EE Developers

2.  Run the following Maven command to create the Java project in the current directory, and input properties when prompted :-

     mvn archetype:generate -DarchetypeGroupId=com.day.jcr.vault -DarchetypeArtifactId=multimodule-content-package-archetype -DarchetypeVersion=1.0.2 -DarchetypeRepository=adobe-public-releases

     groupId:  org.mytest
     artifactId:  mytest
     version:  1.0-SNAPSHOT
     package:  org.mytest
     appsFolderName:  myproject
     artifactName:  My Project
     cqVersion:  6.2
     packageGroup:  My Company

3.  From inside the mytest directory, run the following Maven command :-

     mvn eclipse:eclipse

4.  From Eclipse, goto File menu —-> Import —-> Existing Maven projects —-> mytest directory  ,  and ignore error during import

5.  Create a new Maven Build Run Configuration for the mytest-bundle directory (goals “clean install”) and test it builds JAR in bundle/target directory

6.  In Eclipse Package Explorer, in mytest-bundle, navigate to org.mytest package and add a new class SimplePostHandler  (JAVA CODE BELOW)

7.  Re-build mytest-bundle and check JAR in bundle/target directory has updated

8.  Navigate to http://localhost:4502/system/console/bundles

9.  Click Install/Update button , tick Start Bundle , and click Browse to select your JAR in bundle/target

10.  To test, run the following Curl command and check you get the expected response from your SimplePostHandler :-

      curl --user admin:admin -X POST --data "from=HELLO&to=WORLD" http://localhost:4502/bin/myPostHandler


Below is the Java code needed for step 6 :-


import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;

@SlingServlet(paths="/bin/myPostHandler", methods="POST", metatype=true)
public class SimplePostHandler extends SlingAllMethodsServlet
{
    @Override
    protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
    {
        try
        {
            final String from = request.getParameter("from");
            final String to = request.getParameter("to");
            
            response.getWriter().write("Version 1.1 , from " + from + " to " + to + "\n");
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
    }
}

11 replies

antoniom5495929
New Participant
August 10, 2016
        Why you need to do this? Probably you can use a sling servlet in order to simplify/resolve it.