Expand my Community achievements bar.

SOLVED

Submit form to Servlet CQ5

Avatar

Level 3

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" /> </form>

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"); } }
1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi,
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.

Good Luck
/Johan

View solution in original post

3 Replies

Avatar

Correct answer by
Level 7

Hi,
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.

Good Luck
/Johan

Avatar

Level 3

Thanks a lot Johan for the quick reply. It works. yes