简体   繁体   中英

JSP Servlet getParameter() giving null

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?

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.

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

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

Change addGifts to addgift .

Always check for uppercase ;D

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM