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

S

Saad Malik

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.
 
M

Murray

Saad Malik said:
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.
 
S

Saad Malik

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?
 
J

John C. Bollinger

Saad said:
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top