aspose file tools*
The moose likes Servlets and the fly likes request.setParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.setParameter" Watch "request.setParameter" New topic
Author

request.setParameter

aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Posts: 19
Hi,

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.

Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 254

Hey,
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.

The file control about which I am taking is


Regards
Salil Verma


Regards
Salil Verma
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
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.


Ping & DNS - my free Android networking tools app
aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Posts: 19
Hi,

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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Posts: 19
Thanks Ankit. This will solve my problem
aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Posts: 19
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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...
aniruddha gaikwad
Greenhorn

Joined: Nov 01, 2007
Posts: 19
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Yes in that case you don't should not call the doChain method and use request dispatcher to send the request to the JSP...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.setParameter