Hello!
 Well, it may depend on the way the servlet is being
accessed.
 If through a HTML form or equivalent, and you've got
 <INPUT TYPE="text" name="name" ... >

 If the user click the submit button withouth entering
anything in the name field, it will still send the
value for name, but would be an empty string, which
means that nothing was entered. You might want to
change

 if( name == null )

 for

 if( name.length() == 0 )

 This way you'll test for a 0 length name instead.
Another precaution you should take is testing if there
are spaces in the name field, for example, the user
enters: "            ", which has a length > 0, but
still had nothing inside.  To do this I recommend:

 String NAME = request.getParameter( "name" );
 String Result = "Not entered!";

 NAME.trim(); // this eliminates spaces from both ends
 if( NAME.length() == 0 )
   out.println( Result ); // there's no need to invoke
                          // toString() on a String
obj

 else if( NAME == null )
   out.println( Result );

  else
   out.println( NAME );


We still need to test that NAME is null because:
 a) if the servlet just didn't get the name field
itself
 b) if the servlet is accessed directly (say
http://www.foo.de/servlet/EinflascheServlet?datum=09/12/00&color=red&food=spaghetti)
the servlet's HttpServletRequest will have only those
parameters described AFTER EinflascheServlet, in this
case they would be "datum", "color" and "food".  If
you try to getParemter("name") on this one, it would
be null.

 Hope this helps!
 Daniel

--- Theodor Goulas <[EMAIL PROTECTED]>
wrote:
> Hello everyone,
>
> I wrote a simple servlet application which take the
> user input from a
> web page and return it to a html page.
> The problem is by testing if the value is null or
> not.
> It suppose to return some massage if the value is
> null, let's say "No
> value" and the value, if it's not null.
> Here is some source code :
>
> import java.io.*;
> import java.lang.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class EinfachesServlet extends HttpServlet{
>
>   public void doPost (HttpServletRequest request,
>    HttpServletResponse response)
>    throws ServletException, IOException {
>
>      response.setContentType("text/html");
>
>   PrintWriter out = response.getWriter();
>
>
>
out.println("<HTML><HEAD><title>Example</title></HEAD>"
> + "<body
> bgcolor=FFFFFF>");
>
>     out.println("<table >");
>     out.println("<tr>");
>     out.println("<th></th>");
>     //out.println("<th></th>");
>     out.println("</tr> ");
>     out.println("<tr> ");
>
>     String NAME   = request.getParameter("Name" );
>      String Result = "No value";
>
>        if (NAME==null){
>          out.println("<td>"+ Result.toString() +
> "</td> ");
>       } else {
>             out.println("<td>Name
> entered:"+NAME.toString()+ "</td>");
>         }
>
>          out.println("</tr>");
>        out.println("<tr>");
>
>     String DATE = request.getParameter("Datum");
>
>       if (DATE !=null){
>        out.println("<td>Date entered:"+ DATE +
> "</td>");
>      } else {
>           out.println("<td>No value</td>");
>         }
>      out.println("</tr>");
>       out.println("</table> ");
>
>
>   out.println("Return to: <A
> HREF=../Projekt.html>Projekt </A>");
>
>    out.println("</body></HTML>");
>    out.close();
>   }
> }
>
> If there is not input I get the message "Name
> entered: " instead of  "No
> value".
> What should I change ? The first time I used ...if
> (Name != null) {...do
> something} just  like the second part with the date.
> The second time I tried to make an Object using the
> new(String Name =
> new String(request.getParameter("Name") )
> but it steel didn't work fine.
> Please send me some solutions.
> Thank you very much.
>
>
___________________________________________________________________________
> 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


__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/

___________________________________________________________________________
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