Cannot get FORM Data | Community
Skip to main content
Level 3
June 7, 2022

Cannot get FORM Data

  • June 7, 2022
  • 5 replies
  • 3312 views

I have created a form in aem using core form container and have defined two fields username and password.I want to get this data inside a servlet.

 

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
String groupName="Janhavi";
String userName = request.getRequestParameter("userName").getString();
String password= request.getRequestParameter("password").getString();
 
this is my function to get username and password. When I am creating static user and password the code works fine but when I use the above mentioned function I am not able to get data from the Form.
 
is there any issue in request.getRequestParameter or is there any other method to retrive Form Data?
 
Thank you in advance
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

5 replies

MayurSatav
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 7, 2022

Hi @janhavi_singh ,

 

Have you tried request.getParameter() method ?

String userName = request.getParameter("userName");
String password = request.getParameter("password");

we usually use getParamter() only, could you please check?

Level 3
June 7, 2022

I tried using request.getParameter but still can't data from Form

MayurSatav
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
June 7, 2022

Could you please share your servlet code?

sravs
Community Advisor
Community Advisor
June 7, 2022

@janhavi_singh ,

 

Can you please tell me how are you passing the form data to the servlet call?

Level 3
June 7, 2022

I have created a form which looks like this and have passed the endpoint for the servlet in the core form container 

sravs
Community Advisor
Community Advisor
June 7, 2022

@janhavi_singh ,

 

Please check the name in the form field dialog as shown in the below image is matching with your request parameter name or not. This might be a reason for this.

 

If this is also not the case, then check for the servlet call while submitting the form in network tab from developer console and see what is the payload for that.

 

Regards,

Sravani

 

Adobe Employee
June 7, 2022
Level 3
June 7, 2022

I think this method is valid only for the adaptive forms and not the core form container component of AEM

Adobe Employee
June 7, 2022

Noted! I missed that. I would suggest checking service user permissions.

arunpatidar
Community Advisor
Community Advisor
June 7, 2022

Hi,

How does your form HTML looks like?

Arun Patidar
Level 3
June 7, 2022

I am using core form container and have not made any changes in the html .

arunpatidar
Community Advisor
Community Advisor
June 7, 2022

Can you check if the input fields has the values and name attribute when form is submitted 



<div class="container responsivegrid">
  <form method="POST" action="/content/core-components-examples/library/core-form/form-text.html" id="new_form" name="new_form" enctype="multipart/form-data" class="cmp-form aem-Grid aem-Grid--12 aem-Grid--default--12">
    <input type="hidden" name=":formstart" value="/content/core-components-examples/library/core-form/form-text/jcr:content/root/responsivegrid/demo/component/container">
    <input type="hidden" name="_charset_" value="utf-8">
    <div class="text aem-GridColumn aem-GridColumn--default--12">
      <div class="cmp-form-text">
        <label for="form-text-991299145">Name</label>
        <input class="cmp-form-text__text" data-cmp-hook-form-text="input" type="text" id="form-text-991299145" placeholder="Please enter your name" name="firstName">
      </div>
    </div>
    <div class="text aem-GridColumn aem-GridColumn--default--12">
      <div class="cmp-form-text" data-cmp-required-message="You need to add your email to log in">
        <label for="form-text-2014401237">E-Mail</label>
        <input class="cmp-form-text__text" data-cmp-hook-form-text="input" type="email" id="form-text-2014401237" placeholder="Please enter your E-Mail" name="email" required="">
      </div>
    </div>
    <div class="text aem-GridColumn aem-GridColumn--default--12">
      <div class="cmp-form-text">
        <label for="form-text-1948726228">Message</label>
        <textarea class="cmp-form-text__textarea" data-cmp-hook-form-text="input" id="form-text-1948726228" placeholder="Please enter your message" name="textarea" rows="2"></textarea>
      </div>
    </div>
  </form>
</div>
Arun Patidar
lukasz-m
Community Advisor
Community Advisor
June 7, 2022

Hi @janhavi_singh,

By default Core Form Container is not supporting any additional servlets to handle POST request. It is handled by com.adobe.cq.wcm.core.components.internal.servlets.CoreFormHandlingServlet.

I am assuming that your custom servlet is never called, when you are submit your form. If you want to use your custom servlet registered under specific path, you will need to customize /apps/core/wcm/components/form/container/v2/container and do some modification in container.html. By some modification, I mean to set attribute action to point to your servlet path, e.g.

<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Copyright 2016 Adobe
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<form data-sly-use.container="com.adobe.cq.wcm.core.components.models.form.Container"
      data-sly-use.grid="com.day.cq.wcm.foundation.model.responsivegrid.ResponsiveGrid"
      method="${container.method}" action="/bin/servlets/registration-form" id="${container.id}" name="${container.name}"
      enctype="${container.enctype}"
      class="cmp-form ${grid.cssClass}">
    <div data-sly-test="${container.errorMessages}"
         data-sly-list.item="${container.errorMessages}"
         class="cmp-form-error">
        <p class="cmp-form-error__item">${item}</p>
    </div>
    <input type="hidden" name=":formstart" value="${resource.path}"/>
    <input type="hidden" name="_charset_" value="utf-8"/>
    <input data-sly-test="${container.redirect}" type="hidden" name=":redirect" value="${container.redirect @ extension='html'}"/>
    <sly data-sly-repeat.paragraph="${grid.paragraphs}"
         data-sly-resource="${paragraph.path @ resourceType=paragraph.resourceType, decorationTagName='div', cssClassName=paragraph.cssClass}"></sly>
    <sly data-sly-resource="${resource.path @ resourceType=container.resourceTypeForDropArea, appendPath='/*', decorationTagName='div', cssClassName='new section aem-Grid-newComponent'}"
         data-sly-test="${wcmmode.edit || wcmmode.preview}"></sly>
</form>

In below thread I have described step by step 2 scenarios how to extend Form Core Container:

  1. Creating custom action - this is correct way, because you are not changing any existing mechanism you will use extension points provided by Form Core Container.
  2. Utilize custom servlet (I am using resource type/selector approach but for path it will be very similar) - this is more destructive option - but of course still possible.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/form-core-component-submission-using-custom-action-type/m-p/438243/highlight/true#M125129

I think you should consider to create your own form container component in case you do not want to use OOTB servlet and it's functionalities. It will be much simpler to control entire form flow.

Level 3
June 8, 2022

I have created custom action using the above link and even tried changing the action to "bin/servlet/registration-form", still no success, could you please tell  how to call servlet in the post.POST.jsp file as the getServlet method is deprecated now.