HttpServletRequest.getParameter("name1") should return "" in the case of a
URL like http://whatever.com/servlets?name1=&name2=someVal
Regardless, this is still problematic, since you probably want that value
to be null.
What I've done to get around this is provide my servlets with the
following method:
private String getParameter(HttpServletRequest req, String parameterName)
throws NullParameterException {
String value = req.getParameter(parameterName);
if ( (value == null) || (value.equals("") )
throw new NullParameterException(parameterName);
return value;
}
And define a NullParameterException with a method like:
public String getParameterName()
and a constructor like the one used above:
public NullParameterException(String parameterName)
so that you can later find out which parameter was not passed. It comes
in handy when you're checking for required form fields and the like.
Hope this helps.
-Rob
On Tue, 13 Apr 1999, Jason Proctor wrote:
> I'm seeing some weird behaviour with parameters that are passed to servlets
> as blank strings, ie in a URL such as
> http://machine/zone/servlet?param1=¶m2=. These parameters end up having
> the value "null" (ie a 4-character string) rather than either a blank
> string or a null pointer, which is what I would expect.
>
> Here's the code -
>
> Enumeration parmNames = inRequest.getParameterNames();
>
> while ( parmNames.hasMoreElements() )
> {
> String key = (String) parmNames.nextElement();
>
> String value = inRequest.getParameter( key );
>
> // value == "null" here
> // in case where parameter is passed as ?name=&name=value
> }
>
> Any ideas anyone? Thanks in advance.
>
>
>
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]
>
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]