1

In the servlet I am trying to get the value of the submit button in the form below using

Servlet code:

String addGifts = request.getParameter("addGifts");

This code gives me attribute addGifts as null. The form is below. The value of temp.get(0) is a (Long) Object.

JSP form:

<%
    int i = gifts.size();
    System.out.println("gifts.size() = " + i);
    int j = 0;
    while (j < i) {
        ArrayList temp = new ArrayList();
        temp = gifts.get(j);
        System.out.println("Gift Id: " + temp.get(0));
        out.println("<tr>");
        out.println("<td>" + temp.get(1) + "</td>");
        out.println("<td>" + temp.get(2) + "</td>");
        out.println("<td>" + temp.get(3) + "</td>");
        out.println("<td><form method=\"POST\" action=\"gift-add\">");
        out.println("<button type=\"submit\" name=\"addgift\" value=\"" + temp.get(0) + "\">Redemm</button>");
        out.println("</form></td>");
        out.println("</tr>");
        j++;
    }
    //System.out.println("<input class=\"text\" value=\"Enter Page\">");
%>

Any ideas why the attribute addGifts is null?

Darve
  • 31
  • 1
  • 4

4 Answers4

2

You have a small typo. Your name in code is

name=\"addgift\"  ----->  addgift

and you are using

   request.getParameter("addGifts"); --->  addGifts

Look at the capital G.

So, both the strings must be same. They are case sensitive.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

It because of typo, try replacing following code in servlet. "G" should be in small case

String addGifts = request.getParameter("addgifts");
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

Change addGifts to addgift.

Always check for uppercase ;D

Lukas Warsitz
  • 1,231
  • 1
  • 11
  • 20
0

It is null since there's no component with name "addGifts" inside the <form> to send to the server.

By the way, it is a bad idea to use the button as the holder of your parameter. At least use a hidden field

<input type="hidden" name="addGifts" value="..." />
<button type="submit" name="addgift" value="Redemn" />

Also, it is a bad idea to use scriptlets in JSP. Try to keep it clean of Java code.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332