Question

I created a Servlet call ShippingDetailsServlet.java and deployed it. I need to submit a HTML form to it. I am not sure what path I should put in the form action. Below is the form.

<form action="/services/mycompany/ShippingDetailsServlet" method="post">
Country: <input type="text" name="country" value="au"><br>
Quantity: <input type="text" name="quantity" value="1">
<cq:include path="./submit" resourceType="foundation/components/form/submit" />

Please let me know what path should I give for the form action so that it can be submitted to the Servlet.

Below is the Servlet.

package mycompany.servlets;

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

import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

@SlingServlet(
        paths={"/services/mycompany/"}
)
@Properties({
        @Property(name="service.pid", value="mycompany.ShippingDetailsServlet",propertyPrivate=false),
        @Property(name="service.description",value="Shipping details servlet", propertyPrivate=false),
        @Property(name="service.vendor",value="mycompany", propertyPrivate=false)
})
public class ShippingDetailsServlet extends SlingAllMethodsServlet
{
    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        //Do something fun here
    }

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        //Do something fun here
        PrintWriter out = response.getWriter();
        out.println("Hello");
    }
}
Was it helpful?

Solution 3

Got the answer to my question from the forums.adobe.com

Answer is as below.

if you have annotated your servlet like this:

@SlingServlet(methods = { "POST" }, paths = "/apps/mycompany/servlets/GenericServlet")

the form shoud post to the same same url as in paths, that is "/apps/mycompany/servlets/GenericServlet"

so if you would change you "paths" line in the servlet to "/services/mycompany/ShippingDetailsServlet" the form would post to that one.

OTHER TIPS

That is not necessarily true. You can deploy servlets on custom paths, but in order to do so you need to modify the "Apache Sling Servlet/Script Resolver and Error Handler" and add the custom path to the "Execution Paths" section.

Also, if this form is going to be deployed to your publish instance, you may want to use a custom path other than /bin/ because CQ has a lot of admin servlets registered under /bin and exposing them to the public may present a security concern.

By default, servlets can be deployed only below the /bin path. Don't put trailing / to the paths and don't add any additional properties. Eg. use following annotation:

@SlingServlet(paths={"/bin/services/mycompany"})
public class ShippingDetailsServlet extends SlingAllMethodsServlet

Path in the form should be the same as in @SlingServlet:

<form action="/bin/services/mycompany" method="post">

And if you really want to create servlet outside the /bin, you need to add appropriate path to the Execution Paths property in Apache Sling Servlet/Script Resolver and Error Handler configuration page in the /system/console/configMgr console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top