Passing a HttpServletRequest Object as a Parameter
In general, I need to upload a large file via an html form calling a servlet, and then manipulate that file with a heavy algorithm and return a xml document as a response.
This whole process takes a lot of time, so in order to avoid the browser timeout connection, I have decided t开发者_如何学Co create an object (extending Thread
) that will perform the time consuming process (uploading the file and manipulate it) in a different thread and that will be able to indicate the progress of the process. To do that, I need to pass the HttpServletRequest
request as an argument to the object.
When I tried to parse the request parameter (I am using apache commons FileUpload), I got the following exception:
"the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null" .
Is it possible to pass a HttpServletRequest
object to an object extending Thread? if so, what is the correct practice of safely doing it?
You shouldn't. When the request object is used in the thread, the request may no longer be valid, and Tomcat might have cleaned it (hence your exception) (of course, assuming your form is indeed a enctype="multipart/form-data"
)
So in order to handle this properly, extract the data from the request and pass it to the new thread. Thus you won't depend on the request object - only on its contents.
Note that Servlet 3.0 adds an option for asynchronous server-side processing which seems to be a good option for you - check it out.
The error that you are getting indicates that the form on the client side may not be set up properly. Make sure that your form is defines like this:
<FORM action="YOUR_SERVLET"
enctype="multipart/form-data"
method="post">
精彩评论