2

How do I access nested POST data in the Java Servlet API? In PHP (which I can't use for this project), I was able to access fields with $ds = $_POST['details']['double_sided'];

I have the following inputs in my form:

    <div class="row">
        <div class="col-sm-6">
            <label for="details_double_sided" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[double_sided]">
                <input type="checkbox" id="details_double_sided" value="1" name="<portlet:namespace />details[double_sided]"> Double Sided
            </label>

            <label for="details_stapled" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[stapled]">
                <input type="checkbox" id="details_stapled" value="1" name="<portlet:namespace />details[stapled]"> Stapled
            </label>

            <label for="details_three_hole_punched" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[three_hole_punched]">
                <input type="checkbox" id="details_three_hole_punched" value="1" name="<portlet:namespace />details[three_hole_punched]"> Three Hole Punched
            </label>
        </div>

        <div class="col-sm-6">
            <label for="details_copied_in_color" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[copied_in_color]">
                <input type="checkbox" id="details_copied_in_color" value="1" name="<portlet:namespace />details[copied_in_color]"> Copied in Color
            </label>

            <label for="details_copied_on_color_page" class="control-label">
                <input type="hidden" value="0" name="<portlet:namespace />details[copied_on_color_page]">
                <input type="checkbox" id="details_copied_on_color_page" value="1" name="<portlet:namespace />details[copied_on_color_page]"> Copied on Color Paper (Please indicate paper color in special instructions)
            </label>
        </div>
    </div>

I've tried multiple methods, such as

String[] details = ParamUtil.get(request, "details[double_sided]", "0")

Object details = request.getAttribute("details");

String[] details = ParamUtil.getParameterValues(request, "details[]");

But they all seem to return null / empty values.

Goldentoa11
  • 1,700
  • 2
  • 17
  • 29
  • What web framework are you using ? Whatever you use I suggest using any serialization mechanism it provides. – Catalin Mar 16 '18 at 13:19
  • have you tried request.getParameter("details[double_sided]"); (assuming that it is referenced by name and not id) – John Kane Mar 16 '18 at 13:27

1 Answers1

0

The function getParameter() requires the name of the field that you input, which your code is <portlet:namespace />details[double_sided].

If this doesn't work, you can get all the parameter names that have been passed to your servlet with getParameterNames() which you can iterate over to check your inputs, or getParameterMap which is map containing all the keys and values that have been posted to your servlet.

Jonathan Coustick
  • 1,127
  • 9
  • 19