Submit a file using POST request in CRX

This simple example shows how we can submit a file using a POST request to a JSP from an html form. I used CRXDE for creating the project.

Create a new project in CRXDE by right clicking on the Project Navigator page and selecting Build->Create Project. Give a project name and the package name that you will be using. I will be using simplePOSTRequest as the project name.

  • A new folder is created under apps with the name simplePOSTRequest. In CRXDE Lite you will find under the content node that a new node is created with the name “simplePOSTRequest”. On the node the slingResourceType property is set which points to simplePOSTRequest/components/sample.
  • In simplePOSTRequest/components/sample, under the apps folder you will find a components folder with html.jsp page already created. This is the page that is invoked when you go to http://localhost:7402/simplePOSTRequest.html.

Now modify the html page and add a simple html form which takes a file as in input and has a submit button.  Save the project.

Now, go to http://localhost:7402/simplePOSTRequest.html and select a file and click upload. You will get a 200 OK status message which means that the request was successful (See image below).

We did not have any POST request handling logic at this point, so where did the file go? If you go to CRXDE Lite, you will see under the simplePOSTRequest node in content node that a new node is created. The name of the node is the name we specified for the File input type in the html form.

If we want to create a JSP file and want the POST request to be redirected to that JSP create a POST.jsp file under apps/simplePOSTRequest/components/sample.

Now if you click the upload button, you will see the new page will be invoked and no nodes will be created in the crx. You can access the form data from the HttpServletRequest or SlingHttpServletRequest object in POST.jsp.

Here is the POST.jsp that displays the submitted file details. (Click to enlarge)