Re: Servlets and form posts/querystring

Discussion in 'Java' started by John C. Bollinger, Jun 27, 2003.

  1. Miguel De Anda wrote:
    > "Sandip Chitale" <> wrote in message
    > news:...
    >
    >>use request.getMethod() to find out if was a GET or a POST or ...
    >>
    >>"Miguel De Anda" <> wrote in message

    >
    > news:<LTKKa.3657$>...
    >
    >>>I've been trying to get started with servlets and I ran into something

    >
    > that
    >
    >>>I can't seem to figure out:
    >>>
    >>>Is there a way to distinguish between parameters passed by querystring

    >
    > and
    >
    >>>those passed by a form post like in ASP?

    >>

    >
    > What if I have a form like this:
    >
    > <form action="blah?displaymode=update&section=3" method="post">
    > ...
    >
    > I know that its probably not good practice, but that's how my boss wants it.
    >
    >


    I wondered whether you were doing something like this. Is there any
    real need to draw a distinction? The ServletRequest parameter access
    methods will provide access to a combination of the query string and
    message body parameters. Would it actually be a problem if parameters
    that were usually provided in the form data were provided in the query
    string instead, or vise-versa? If your application depends on such
    distinctions then it is likely quite brittle.

    If you really do need to perform this sort of check then you can use
    HttpServletRequest.getQueryString() and parse the result yourself.
    Although I hesitate to even mention it, there is also the deprecated
    javax.servlet.http.HttpUtils class that provides methods you might be
    able to use -- but it is deprecated, so I don't recommend it.


    John Bollinger
    John C. Bollinger, Jun 27, 2003
    #1
    1. Advertising

  2. "John C. Bollinger" <> wrote in message
    news:bdhruq$pf2$...
    > Miguel De Anda wrote:
    > > "Sandip Chitale" <> wrote in message
    > > news:...
    > >
    > >>use request.getMethod() to find out if was a GET or a POST or ...
    > >>
    > >>"Miguel De Anda" <> wrote in message

    > >
    > > news:<LTKKa.3657$>...
    > >
    > >>>I've been trying to get started with servlets and I ran into something

    > >
    > > that
    > >
    > >>>I can't seem to figure out:
    > >>>
    > >>>Is there a way to distinguish between parameters passed by querystring

    > >
    > > and
    > >
    > >>>those passed by a form post like in ASP?
    > >>

    > >
    > > What if I have a form like this:
    > >
    > > <form action="blah?displaymode=update&section=3" method="post">
    > > ...
    > >
    > > I know that its probably not good practice, but that's how my boss wants

    it.
    > >
    > >

    >
    > I wondered whether you were doing something like this. Is there any
    > real need to draw a distinction? The ServletRequest parameter access
    > methods will provide access to a combination of the query string and
    > message body parameters. Would it actually be a problem if parameters
    > that were usually provided in the form data were provided in the query
    > string instead, or vise-versa? If your application depends on such
    > distinctions then it is likely quite brittle.
    >
    > If you really do need to perform this sort of check then you can use
    > HttpServletRequest.getQueryString() and parse the result yourself.
    > Although I hesitate to even mention it, there is also the deprecated
    > javax.servlet.http.HttpUtils class that provides methods you might be
    > able to use -- but it is deprecated, so I don't recommend it.
    >
    >
    > John Bollinger
    >
    >


    I see. Thanks a lot. I did notice though, that the querystring parameters
    seem to override the form post. For example, if a form field is named id and
    value of "1" and the querystring somehow has ?id=2, then it seems like I get
    id=2.
    Miguel De Anda, Jun 27, 2003
    #2
    1. Advertising

  3. Miguel De Anda wrote:
    > "John C. Bollinger" <> wrote in message
    > news:bdhruq$pf2$...
    >
    >>Miguel De Anda wrote:
    >>
    >>>"Sandip Chitale" <> wrote in message
    >>>news:...
    >>>
    >>>
    >>>>use request.getMethod() to find out if was a GET or a POST or ...
    >>>>
    >>>>"Miguel De Anda" <> wrote in message
    >>>
    >>>news:<LTKKa.3657$>...
    >>>
    >>>
    >>>>>I've been trying to get started with servlets and I ran into something
    >>>
    >>>that
    >>>
    >>>
    >>>>>I can't seem to figure out:
    >>>>>
    >>>>>Is there a way to distinguish between parameters passed by querystring
    >>>
    >>>and
    >>>
    >>>
    >>>>>those passed by a form post like in ASP?
    >>>>
    >>>What if I have a form like this:
    >>>
    >>><form action="blah?displaymode=update&section=3" method="post">
    >>>...
    >>>
    >>>I know that its probably not good practice, but that's how my boss wants

    >
    > it.
    >
    >>>

    >>I wondered whether you were doing something like this. Is there any
    >>real need to draw a distinction? The ServletRequest parameter access
    >>methods will provide access to a combination of the query string and
    >>message body parameters. Would it actually be a problem if parameters
    >>that were usually provided in the form data were provided in the query
    >>string instead, or vise-versa? If your application depends on such
    >>distinctions then it is likely quite brittle.
    >>
    >>If you really do need to perform this sort of check then you can use
    >>HttpServletRequest.getQueryString() and parse the result yourself.
    >>Although I hesitate to even mention it, there is also the deprecated
    >>javax.servlet.http.HttpUtils class that provides methods you might be
    >>able to use -- but it is deprecated, so I don't recommend it.
    >>
    >>
    >>John Bollinger
    >>
    >>

    >
    >
    > I see. Thanks a lot. I did notice though, that the querystring parameters
    > seem to override the form post. For example, if a form field is named id and
    > value of "1" and the querystring somehow has ?id=2, then it seems like I get
    > id=2.


    I haven't tested this case specifically, but it is likely that you are
    ending up with a multi-valued parameter. From the API docs for
    ServletRequest.getParameter:

    "Returns the value of a request parameter as a String, or null if the
    parameter does not exist. Request parameters are extra information sent
    with the request. For HTTP servlets, parameters are contained in the
    query string or posted form data.

    You should only use this method when you are sure the parameter has only
    one value. If the parameter might have more than one value, use
    getParameterValues(java.lang.String).

    If you use this method with a multivalued parameter, the value returned
    is equal to the first value in the array returned by getParameterValues."

    I believe the Servlet specification has more details on how parameters
    are handled in HttpServlets, and I recommend that you get that and read
    it if you have not already. It is available as a free download from
    Sun. If that is too heavy a read then at least do carefully read the
    relevant API docs.


    John Bollinger
    John C. Bollinger, Jun 30, 2003
    #3
  4. "John C. Bollinger" <> wrote in message
    news:bdpn2d$9v2$...
    > Miguel De Anda wrote:
    > > "John C. Bollinger" <> wrote in message
    > > news:bdhruq$pf2$...
    > >
    > >>Miguel De Anda wrote:
    > >>
    > >>>"Sandip Chitale" <> wrote in message
    > >>>news:...
    > >>>
    > >>>
    > >>>>use request.getMethod() to find out if was a GET or a POST or ...
    > >>>>
    > >>>>"Miguel De Anda" <> wrote in message
    > >>>
    > >>>news:<LTKKa.3657$>...
    > >>>
    > >>>
    > >>>>>I've been trying to get started with servlets and I ran into

    something
    > >>>
    > >>>that
    > >>>
    > >>>
    > >>>>>I can't seem to figure out:
    > >>>>>
    > >>>>>Is there a way to distinguish between parameters passed by

    querystring
    > >>>
    > >>>and
    > >>>
    > >>>
    > >>>>>those passed by a form post like in ASP?
    > >>>>
    > >>>What if I have a form like this:
    > >>>
    > >>><form action="blah?displaymode=update&section=3" method="post">
    > >>>...
    > >>>
    > >>>I know that its probably not good practice, but that's how my boss

    wants
    > >
    > > it.
    > >
    > >>>
    > >>I wondered whether you were doing something like this. Is there any
    > >>real need to draw a distinction? The ServletRequest parameter access
    > >>methods will provide access to a combination of the query string and
    > >>message body parameters. Would it actually be a problem if parameters
    > >>that were usually provided in the form data were provided in the query
    > >>string instead, or vise-versa? If your application depends on such
    > >>distinctions then it is likely quite brittle.
    > >>
    > >>If you really do need to perform this sort of check then you can use
    > >>HttpServletRequest.getQueryString() and parse the result yourself.
    > >>Although I hesitate to even mention it, there is also the deprecated
    > >>javax.servlet.http.HttpUtils class that provides methods you might be
    > >>able to use -- but it is deprecated, so I don't recommend it.
    > >>
    > >>
    > >>John Bollinger
    > >>
    > >>

    > >
    > >
    > > I see. Thanks a lot. I did notice though, that the querystring

    parameters
    > > seem to override the form post. For example, if a form field is named id

    and
    > > value of "1" and the querystring somehow has ?id=2, then it seems like I

    get
    > > id=2.

    >
    > I haven't tested this case specifically, but it is likely that you are
    > ending up with a multi-valued parameter. From the API docs for
    > ServletRequest.getParameter:
    >
    > "Returns the value of a request parameter as a String, or null if the
    > parameter does not exist. Request parameters are extra information sent
    > with the request. For HTTP servlets, parameters are contained in the
    > query string or posted form data.
    >
    > You should only use this method when you are sure the parameter has only
    > one value. If the parameter might have more than one value, use
    > getParameterValues(java.lang.String).
    >
    > If you use this method with a multivalued parameter, the value returned
    > is equal to the first value in the array returned by getParameterValues."
    >
    > I believe the Servlet specification has more details on how parameters
    > are handled in HttpServlets, and I recommend that you get that and read
    > it if you have not already. It is available as a free download from
    > Sun. If that is too heavy a read then at least do carefully read the
    > relevant API docs.
    >
    >
    > John Bollinger
    >
    >



    Oh, so the getParameterValues() is good for like checkboxes or radio buttons
    with the same name right? (That actually answers my next question.)
    Miguel De Anda, Jun 30, 2003
    #4
  5. Miguel De Anda wrote:
    >
    > Oh, so the getParameterValues() is good for like checkboxes or radio buttons
    > with the same name right? (That actually answers my next question.)
    >


    I'd have to check the HTML spec for the expected behavior in those
    cases. If I recall correctly then for checkboxes you would get one
    parameter (if any of the like-named boxes was checked) with one or more
    empty values. For radio buttons I think the whole point is that you
    only ever get the value associated with the selected option.


    John Bollinger
    John C. Bollinger, Jul 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mehdi
    Replies:
    6
    Views:
    36,074
    sloan
    Apr 6, 2006
  2. John Ryan
    Replies:
    1
    Views:
    359
    Toby A Inkster
    Nov 15, 2003
  3. KhurramAtk
    Replies:
    1
    Views:
    222
    Paul Glavich [MVP ASP.NET]
    Mar 26, 2005
  4. Adeel Ahmad
    Replies:
    1
    Views:
    356
    Anthony Jones
    Mar 7, 2006
  5. Billy
    Replies:
    1
    Views:
    118
    Mike Brind
    Apr 15, 2006
Loading...

Share This Page