0

I'm trying to create a simple html form that is processed with a sling servlet. It's a very basic form with a couple input fields and my java class is pretty straightforward. All I'm trying to do is log the values from the form in my error log. Instead simply nothing happens. I must be missing something small or just plain blind.

Form html

<form name="" method="POST" action="/apps/form">
 <input id="firstName" type="text" name="firstName" />
 <input id="phoneNumber" type="text" name="phoneNumber" />          
 <input type="submit" name="submit" value="submit">
</form>

Java Class

@Component(immediate=true, metatype=false, label="FORM SERVLET")

@Properties({
@Property(name="sling.servlet.methods", value={"POST"}),
@Property(name="sling.servlet.paths", value={"/apps/form"}),
@Property(name="sling.servlet.selectors", value={"form"}),
@Property(name="sling.servlet.extensions", value={"html"})
})

public class FormServlet extends SlingSafeMethodsServlet {

    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String name = request.getParameter("firstName");
    String phone = request.getParameter("phoneNumber");

    out.println(name);
    out.println(phone);

log.error("the chat component is loaded first name" + name + phone);

}

}

Delmon Young
  • 2,013
  • 5
  • 39
  • 51

2 Answers2

1

You can register servlets to a particular path, in that case the extensions and selectors are irrelevant. normally you register paths under the /bin directory

If /apps/form exists you should register your servlet to respond to that resourceType instead of the path. for example:

@SlingServlet(
    description = "processes a form",
    resourceTypes = { "yourapp/component" },
    selectors = { "form" },
    extensions = { "html" },
    methods = { "POST" })
public class FormServlet extends SlingAllMethodsServlet { ... }
santiagozky
  • 2,519
  • 22
  • 31
  • thanks for the reply except I'm kind of confused. If I register a path under the bin directory shouldn't I be able to navigate there? Anytime I do that I simply get a 404. Am I missing something here? – Delmon Young May 07 '13 at 14:12
  • your servlet is configured to respond to POST request. if you access it through your browser you are sending a GET request, which wont find any match – santiagozky May 07 '13 at 14:13
  • thanks @santiagozky, using resourceTypes instead of sling.servlet.paths seemed to do the trick. Thanks for the help. – Delmon Young May 08 '13 at 00:56
  • you're welcome. I recommend you to check the sling servlet documentation. you can see the difference between registering for a path or a resourceType https://sling.apache.org/site/servlets.html – santiagozky May 08 '13 at 09:09
0

are you able to reach http://yourmachine.com/apps/form by typing in the browser to make sure your Servelet is fine

then try passing the parmeter on the URL itself http://localhost/apps/form?firstName="anish"&phoneNumber="22222222222"

if all goes fine then your servelet is Ok

Then you have to figure out the view enable the Firefox/chrome developer tool to figure out where the URL is going

Hope it helps

anish
  • 6,884
  • 13
  • 74
  • 140