Dangerous to forward using dispatcher w/ uploaded files???

Discussion in 'Java' started by Saad Malik, Jul 5, 2004.

  1. Saad Malik

    Saad Malik Guest

    Hi Guys/Gals,
    I know that when you upload files in JSP/Servlets you have to set the
    form method to post and type to form-data.

    Since the method is POST, the file will be attached after the request
    headers of the client request.

    And if that is true, then is it also true that you SHOULD never
    forward to servlets/jsp (using requestDispatcher) since there could be
    a potential 100's of mb in the body of the request that has to be
    moved with the original request to the new servlet/jsp?

    If this is not the case, can someone please explain why it is not?

    THanks.

    Saad.
    Saad Malik, Jul 5, 2004
    #1
    1. Advertising

  2. Saad Malik

    Murray Guest

    "Saad Malik" <> wrote in message
    news:...
    > Hi Guys/Gals,
    > I know that when you upload files in JSP/Servlets you have to set the
    > form method to post and type to form-data.
    >
    > Since the method is POST, the file will be attached after the request
    > headers of the client request.
    >
    > And if that is true, then is it also true that you SHOULD never
    > forward to servlets/jsp (using requestDispatcher) since there could be
    > a potential 100's of mb in the body of the request that has to be
    > moved with the original request to the new servlet/jsp?
    >
    > If this is not the case, can someone please explain why it is not?
    >
    > THanks.
    >
    > Saad.


    Most of what you have said is true, i.e. the file is sent as POST data and
    will be forwarded as part of the request. However, it should have no/little
    impact on performance since forwards using requestDispatcher are done
    internally. It doesn't go back to the client, it doesn't copy/recreate a new
    request, it should simply pass the existing request by reference to the
    servlet you're forwarding to. At least that's how it works for application
    servers that I've used.

    There may be one benefit from using sendRedirect instead of forward() .. the
    POSTed data MAY be eligible for garbage collection slightly sooner. But this
    would only be an issue if memory is very scarce and/or the servlet you're
    forwarding to takes a long time to complete.
    Murray, Jul 5, 2004
    #2
    1. Advertising

  3. Saad Malik

    Saad Malik Guest

    > There may be one benefit from using sendRedirect instead of forward() .. the
    > POSTed data MAY be eligible for garbage collection slightly sooner. But this
    > would only be an issue if memory is very scarce and/or the servlet you're
    > forwarding to takes a long time to complete.


    Thanks for responding Murray. I didn't even think about that you pass
    request and response object as a reference to another servlet with
    dispatcher.

    However you made me even more confused now :(

    response.sendRedirect() sets a status code to 303 something (I think)
    and also sends back a header to the client to connect to another URL.
    However, new REQUEST & Response ARE generated since they are not
    passed to the new servlet/jsp.

    On top of that, doesn't sendRedirect not send query data even of a get
    request... unless you specically append it e.g. sendRedirect(afs.jsp?
    + "fsf=dfs");

    Making it is impossible to redirect a request that was tranmitted with
    post using sendRedirect()... right?

    So the only solution left is RequestDispatcher..

    Just wondering if i'm wrong or right?
    Saad Malik, Jul 5, 2004
    #3
  4. Saad Malik wrote:

    >>There may be one benefit from using sendRedirect instead of forward() .. the
    >>POSTed data MAY be eligible for garbage collection slightly sooner. But this
    >>would only be an issue if memory is very scarce and/or the servlet you're
    >>forwarding to takes a long time to complete.

    >
    >
    > Thanks for responding Murray. I didn't even think about that you pass
    > request and response object as a reference to another servlet with
    > dispatcher.
    >
    > However you made me even more confused now :(
    >
    > response.sendRedirect() sets a status code to 303 something (I think)
    > and also sends back a header to the client to connect to another URL.


    It is probably clearest (and most consistent with the HTTP specs) to
    characterize this as the server sending a response with status code 303.
    That might be called a "redirect response", or, less formally, simply a
    "redirect".

    > However, new REQUEST & Response ARE generated since they are not
    > passed to the new servlet/jsp.


    Yes, an HTTP user agent (such as a web browser) will typically handle a
    redirect response by generating a new request for the specified
    alternative URI. Sending a redirect thus causes a second HTTP
    round-trip, which is the one of the fundamental differences between
    redirecting and forwarding with a RequestDispatcher.

    > On top of that, doesn't sendRedirect not send query data even of a get
    > request... unless you specically append it e.g. sendRedirect(afs.jsp?
    > + "fsf=dfs");


    This is probably correct, and it is in any case the appropriate behavior.

    > Making it is impossible to redirect a request that was tranmitted with
    > post using sendRedirect()... right?


    That depends on how the user agent handles the situation. I don't
    offhand know what the more common ones do, but conceivably they might
    repost the data.

    > So the only solution left is RequestDispatcher..


    You say that as if it's a bad thing. If you're talking about a
    situation where large posts are being handled by your application then a
    RequestDispatcher.forward() is exactly what you want. In fact, in many
    cases it is exactly what you want. In most others the underlying
    problem is that HTTP does not support the functionality you want; Java
    has no control over that.


    John Bollinger
    John C. Bollinger, Jul 5, 2004
    #4
    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. Matt G
    Replies:
    1
    Views:
    1,148
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. Maciek Zywno
    Replies:
    0
    Views:
    848
    Maciek Zywno
    Sep 3, 2003
  3. Tony Murphy
    Replies:
    0
    Views:
    1,529
    Tony Murphy
    Feb 16, 2004
  4. Replies:
    2
    Views:
    722
  5. Kai Krakow
    Replies:
    3
    Views:
    149
    Kai Krakow
    May 15, 2008
Loading...

Share This Page