JSP/Servlet: Is Form field Defined

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DiggidyMack69

    JSP/Servlet: Is Form field Defined

    Hello folks I am working on a project using JSP's and Servlets. I have
    a servlet that handles requests from several dynamic HTML forms. I
    want to know if there is a way to check wether an HTML form field was
    defined. In ColdFusion there is an isDefined() function. Right now I
    am checking wether the request.getPara meter("formfiel d") is null.
    this has problems with fields left empty as nulls mean something in
    this form. What is the best way to accomplish this??

    DM
  • Joe

    #2
    Re: JSP/Servlet: Is Form field Defined

    In article <c86ce4f.030922 0642.391dd891@p osting.google.c om>,
    DiggidyMack69@h otmail.com says...[color=blue]
    > I want to know if there is a way to check wether an HTML form field was
    > defined.[/color]

    I think I'd do it this way:


    public isDefined(HttpS ervletRequest req, String fieldNameToSear chFor) {
    return req.getParamete rMap().contains Key(fieldNameTo SearchFor);
    }











    --

    Fuck George W. Bush!!!

    Comment

    • Joe

      #3
      Re: JSP/Servlet: Is Form field Defined

      In article <MPG.19d8b97cf6 43c01e989838@sf o.news.speakeas y.net>,
      [email protected] et says...[color=blue]
      > In article <c86ce4f.030922 0642.391dd891@p osting.google.c om>,
      > DiggidyMack69@h otmail.com says...[color=green]
      > > I want to know if there is a way to check wether an HTML form field was
      > > defined.[/color]
      >
      > I think I'd do it this way:
      >
      >
      > public isDefined(HttpS ervletRequest req, String fieldNameToSear chFor) {
      > return req.getParamete rMap().contains Key(fieldNameTo SearchFor);
      > }
      >
      >[/color]

      oops, I meant:

      public boolean isDefined(HttpS ervletRequest req, String fieldToSearchFo r)
      {
      return req.getParamete rMap().contains Key(fieldToSear chFor);
      }

      Comment

      • Nathan Zumwalt

        #4
        Re: JSP/Servlet: Is Form field Defined

        It sounds like you already have the solution to this problem... check
        for null. Fields that are left empty will return an empty string
        ("").

        Alternatively, you could have a hidden input field that lists all the
        fields on the screen. When it submits, parse this field first to get
        your "valid" parameters.

        -Nathan

        DiggidyMack69@h otmail.com (DiggidyMack69) wrote in message news:<c86ce4f.0 309220642.391dd [email protected] gle.com>...[color=blue]
        > Hello folks I am working on a project using JSP's and Servlets. I have
        > a servlet that handles requests from several dynamic HTML forms. I
        > want to know if there is a way to check wether an HTML form field was
        > defined. In ColdFusion there is an isDefined() function. Right now I
        > am checking wether the request.getPara meter("formfiel d") is null.
        > this has problems with fields left empty as nulls mean something in
        > this form. What is the best way to accomplish this??
        >
        > DM[/color]

        Comment

        • DiggidyMack69

          #5
          Re: JSP/Servlet: Is Form field Defined

          Joe <sfjoe@spamcop. net> wrote in message news:<MPG.19d8b 97cf643c01e9898 [email protected] akeasy.net>...[color=blue]
          > In article <c86ce4f.030922 0642.391dd891@p osting.google.c om>,
          > DiggidyMack69@h otmail.com says...[color=green]
          > > I want to know if there is a way to check wether an HTML form field was
          > > defined.[/color]
          >
          > I think I'd do it this way:
          >
          >
          > public isDefined(HttpS ervletRequest req, String fieldNameToSear chFor) {
          > return req.getParamete rMap().contains Key(fieldNameTo SearchFor);
          > }[/color]


          Joe, your a life saver. before I saw your post I popped all of the
          results into an enumeration list and checked them manually. Your ways
          is Definately much better. Thanks


          public boolean isFormFieldDefi ned(HttpServlet Request request, String
          x)
          throws ServletExceptio n, IOException
          {
          String tests = "a";
          String chkfield = x;
          boolean def = false;
          Enumeration enum = request.getPara meterNames();
          while (enum.hasMoreEl ements())
          {
          tests = (String)enum.ne xtElement();
          if(tests.equals (chkfield))
          {
          def = true;
          break;
          }
          }
          return def;
          }//end isFormFieldDefi ned.

          Comment

          Working...