Expand my Community achievements bar.

Join Cristinel Anastasoaie, Timothy Furlow, and Brent Kostak, WEDNESDAY, 2/28/24 @8am PT for the next Adobe Target Community Q&A Coffee Break on topics from the recent High Performance Recommendations Webinar PT 2 of the ongoing Target Personalization Maturity Webinar Series
SOLVED

When I submitted the form, I got a Forbidden error. How can I fix this?

Avatar

Level 1

HTL CODE 

<div data-sly-use.submitForm="com.aem.projectt.example.core.models.RegistrationModel">
<form action="${submitForm.action}" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<br/><br/>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br/><br/>
<button type="submit">Submit</button>
</form>
</div>

RegistrationModel

 

package com.aem.projectt.example.core.models;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class RegistrationModel {

@SlingObject
private SlingHttpServletRequest request;

@ValueMapValue
private String heading;

public String getHeading() {
return heading;
}

public String getAction() {
return request.getResource().getPath() + "/submitForm";
}

}

Sling Servlet 

@Component(service = { Servlet.class },
property = {
"sling.servlet.paths=/bin/custom/saveFormData" ,
"sling.servlet.methods={GET,POST}",
"sling.servlet.selectors={one,two}",
"sling.servlet.extensions=txt"
})
public class SlingServletByPath extends SlingAllMethodsServlet {

private static final Logger LOG = LoggerFactory.getLogger(SlingServletByPath.class);

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
LOG.info("Value of Name: {}", name);
LOG.info("Value of Email: {}", email);
response.getWriter().write("Hello World" + " " + name +" "+ email);
}

}

Screenshot from 2023-12-14 15-32-35.png

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi @Utsav_Dabhi 

You have registered your servlet with path: /bin/custom/saveFormData but you have retrurned the form action as request.getResource().getPath() + "/submitForm", which is why the correct servlet is not getting called.

Update the getAction method RegistrationModel class:

public String getAction() {
return "/bin/custom/saveFormData";
}

 

Ensure that your servlet is registered correctly and available in OSGi. 

 

-Vikas Chaudhary

View solution in original post

1 Reply

Avatar

Correct answer by
Level 3

Hi @Utsav_Dabhi 

You have registered your servlet with path: /bin/custom/saveFormData but you have retrurned the form action as request.getResource().getPath() + "/submitForm", which is why the correct servlet is not getting called.

Update the getAction method RegistrationModel class:

public String getAction() {
return "/bin/custom/saveFormData";
}

 

Ensure that your servlet is registered correctly and available in OSGi. 

 

-Vikas Chaudhary