Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list

How to read checkbox values from the HTML form data in AEM | AEM Servlet | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

How to read checkbox values from the HTML form data in AEM | AEM Servlet by Keshav Chaurasiya

Abstract

Group checkbox values from the HTML form data in to AEM Servlet
countriesForm.html

ServletFormDataCheckbox.class
package com.kkc.core.servlets

import com.day.cq.commons.jcr.JcrConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.ServletResolverConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.propertytypes.ServiceDescription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Component(service = Servlet.class, immediate = true, property = { "description=Form Data Checkbox Servlet",
ServletResolverConstants.SLING_SERVLET_METHODS + "="
+ HttpConstants.METHOD_POST,
ServletResolverConstants.SLING_SERVLET_PATHS + "=/bin/countryForm" })
public class ServletFormDataCheckbox extends SlingAllMethodsServlet {

private static final long serialVersionUID = 1L;
private static Logger log = LoggerFactory.getLogger(ServletFormDataCheckbox.class);

@Override
protected void doPost(final SlingHttpServletRequest req,
final SlingHttpServletResponse resp) throws ServletException, IOException {
final Resource resource = req.getResource();
resp.setContentType("text/plain");
resp.getWriter().write("Title = " + resource.getValueMap().get(JcrConstants.JCR_TITLE));

String[] values = req.getParameterValues("country");

log.info("Selected Countries are :");

for( String value : values )
{
log.info(" country name "+ value);
}
}
}

Read Full Blog

How to read checkbox values from the HTML form data in AEM | AEM Servlet

Q&A

Please use this thread to ask the related questions.

0 Replies