This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I m working on a program to upload a file and the file will submit the servlet for processing. This specific servlet can't handle the file coming in request, so I wrote a program which get the uploaded file, read that file and then file content store in the string and then submit the string to the servlet. My problem is I need to send the file content to a servlet as parameter, Is there any way to add the parameter in the request or using RequestWrapper I can add the parameter to request. Because there is no setParamter method for request object.
As per my understanding, you want to upload the files from client side and process it at the server side. why don't you use "input file" html control in your form, it will add the corresponding parameter in the request.
I don't fully understand how the flow works (or why the servlet couldn't be altered to do the file processing), but the way to pass additional information is to use a request attribute via setAttribute and getAttribute.
Joined: Nov 01, 2007
I used the file control.
I couldn't use (add/modify) the Servlet because, the servlet code was already developed and legally not possible to alter it. And I need to pass the file data to that servlet. The flow is I m passing(uploading) file to a jsp, process it and get the data from it using Apache Common file upload lib. And required to pass that file string to servlet. And as we cant change the code it is not possible to use setAttribute / getAttribute
I know of a dirty trick to do this. What you need to do is to create an instance of an anonymous inner class which is of type HttpServletRequestWrapper from your request (assuming your request is HTTP request). Your anonymous inner class will override the getParameter method, check the name of the request parameter an if it matches your custom request parameter, then you return a custom value, otherwise you call the getParameter method of the super class i.e. the original getParameter method in HttpServletRequestWrapper class. This is a dirty method as this custom parameter will not be there in the getParamaternames or getParameterValues methods...
Thanks Ankit, its working I override getParameterNames, getParameter and getParameterMap. And use the servlet to pass the RequestWrapper, its working fine. But can I use the RequestDispatcher and instead of going to the next servlet/filter again go back to the jsp
aniruddha gaikwad wrote:But can I use the RequestDispatcher and instead of going to the next servlet/filter again go back to the jsp
This is not perfectly clear, can you please clarify a little more...
Joined: Nov 01, 2007
I m using filter to wrap the request, and from that filter i called the doChain method to go to next servlet/filter. I m submitting the file alongwith some more parameters, if in filter some condition is failed lets say file size is above limit, I do want to stop the chain and redirect the request to other jsp.