On Wed, 4 Apr 2001, irene_plj asiamail.com wrote:

> Hi all ,
>
> The problem wf the below code(part of my servlet)is i always get the
> "if" condition even if it "p" equals null.
> Wat could be wrong???
> This is such a simple problem but i jus cannot get the "else"
> condition when p==null.
> Anyone any idea why this cannot work??

Are you sure p is null?  Or is it just empty (i.e. the empty string,
"")?  Because they are different.

I checked the API javadocs at java.sun.com, and this is what it has to
say about getParameter():

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

But you need to be careful about what "does not exist" means.  Again,
existing with an empty value is not the same as not existing.

I did a quick test, making an HTML page with a form with a textfield
whose name was "T", and left it blank when submitting it, and the
query string showed up as "T=".  I interpret that as the parameter "T"
existing, but being empty.

Your solution might be as simple as changing your conditional test to

  if (p != null && ! p.equals(""))

or alternatively

  if (p != null && p.length() > 0)

BTW, the API was specific about what getParameter() would return
(i.e. when it was null), but the spec wasn't (I'm talking about 2.2; I
checked the 2.3 spec, and it does mention it, because it has a section
on the "API Details").


> ******************************************
> ..
> ..
> try
> {
> String p=request.getParameter("ID");
> if(p != null)
> {
>   String to=p+"@asiamail.com";
> }
> else
> {
>   String to="[EMAIL PROTECTED]";
> }
>
> }
> ..
> ..
> **********************************************

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to