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?

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You mean name=\"addGifts\", otherwise it will still be null for being different Strings.
Wow. Didn't notice that. Thanks so much!
@Darve. Happens sometime. Happy coding :)
0

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

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

Comments

0

Change addGifts to addgift.

Always check for uppercase ;D

Comments

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:

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.