The Artima Developer Community
Sponsored Link

Java Answers Forum
getParameter returns null even though checkbox is checked!

8 replies on 1 page. Most recent reply: Sep 4, 2003 3:06 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Stephen Huey

Posts: 10
Nickname: stephen
Registered: Aug, 2003

getParameter returns null even though checkbox is checked! Posted: Sep 2, 2003 11:45 AM
Reply to this message Reply
Advertisement
My JSP has a form, and when it's submitted, some JSP code at the top of the page handles it with this:

if(request.getMethod().equalsIgnoreCase("POST")) {

etc, etc.

Most of the form fields are being extracted on the back end in a bean, but now I need to add a checkbox to see which bean to use, so I'm trying to do it in the JSP right after the code above:


if(request.getMethod().equalsIgnoreCase("POST")) {
String saveAddrString = request.getParameter("saveaddresses");
out.println("saveaddresses is " + saveAddrString);

etc, etc.

The checkbox HTML code looks like this:

<td>Check this box to save them: <input type='checkbox' name='saveaddresses' value='yes'></td>

The println statement above says that saveAddrString is null even when I check the box! How could this be?

Update: Ok, so I just don't understand something about getting info out of those form fields, because all of the other fields are null as well. Back in the bean code, it gets all that by parsing through request.getInputStream(), so I'm wondering if my code above is attempting to call request.getParameter at a point where that parameter is already reset. That of course would make me wonder why getMethod still returns POST, but no matter...I can swallow that they would follow different rules. So, if getParameter won't work at that point in my code, then is there anything else I can do (client-side Javascript?) to make sure I can check for that field's value in the JSP code before I choose which bean to pass everything to?


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 8:41 AM
Reply to this message Reply
Coool....it works for me...
just make sure if the method is POST ...because once i was setting method as post but was getting in JSP as get...sounds wierd but that is/
out.println("Method "+request.getMethod());

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 8:50 AM
Reply to this message Reply
Use this

<FORM ACTION=checkresult1.jsp method="POST" >

see method=post instead of type=post it will work

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 4:49 PM
Reply to this message Reply
if(request.getMethod().equalsIgnoreCase("POST")) { 
String saveAddrString = request.getParameter("saveaddresses"); 
out.println("saveaddresses is " + saveAddrString);


but from the above lines it looks to me that the method is indeed POST since the out.println() line seems to be inside the if statement and it seems to be executing since he/she gets an output. But the output is null. I am a little confuced too about this problem.

Raji

Posts: 5
Nickname: rajisubbu
Registered: Sep, 2003

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 9:57 PM
Reply to this message Reply
hi,
try casting return value to string..Sometimes it returns null if you do not cast to string.<br>
Try <b>(String)request.getParameter("saveaddresses");</b>
when getting the value.It should work

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 11:54 PM
Reply to this message Reply
I disagree with Rajarajeshwari's comments. I have given below the method Signature of getParameter and you can see for yourself why I disagree.

<b>
getParameter
</b>
public java.lang.String getParameter(java.lang.String name) 

Returns the value of a request parameter as a String, or null if the parameter does not exist.

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: getParameter returns null even though checkbox is checked! Posted: Sep 3, 2003 11:59 PM
Reply to this message Reply
I tooo disagreee

Raji

Posts: 5
Nickname: rajisubbu
Registered: Sep, 2003

Re: getParameter returns null even though checkbox is checked! Posted: Sep 4, 2003 12:16 AM
Reply to this message Reply
I am aware that request.getParameter() returns String.

it might not sound logical..
but we faced same problems and got the value by just casting sometimes..

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: getParameter returns null even though checkbox is checked! Posted: Sep 4, 2003 3:06 AM
Reply to this message Reply
If a method returns a String it can return a String or null. I really don't understand what did you are trying to achieve by casting it to a String again? There should be some reason behind it. If there is no logical reason what kind of reason is it?

Can you provide with code segments for cases where it only worked with casting to Strings?

I guess becuase something works without knowing why it works applying it will cause more problems in the future.

I think this is something you should read.

http://www.pragmaticprogrammer.com/ppbook/extracts/coincidence.html

Flat View: This topic has 8 replies on 1 page
Topic: Reading Images from JTextPane Previous Topic   Next Topic Topic: JNDI-NAME and EJB-REF

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use