Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes request.getParameter( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getParameter("fileName") returning null in post method of servlet . Why?" Watch "request.getParameter("fileName") returning null in post method of servlet . Why?" New topic
Author

request.getParameter("fileName") returning null in post method of servlet . Why?

Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Hi
When i try to read one of my html field(of type file) value from within the post method of HttpServlet its returning null.

My html code..



My servlet code..



Output: null

Any clue for this???
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Are you trying to get the contect of the file or the filename?

Unfortunately, servlet doesn't provide a way to do that. You can take a look at Commons FileUpload.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
just file name and moreover here i was asked not to go with any third party jars.
Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
The Servlet and JSP specifications does not provide us with any special feature to handle multipart requests (file uploads).

However,we can use third party libraries or make their own framework for this.Writing our own code to handle this is just like re-inventing the wheel. As Freddy Wong suggested we can use package at http://commons.apache.org/fileupload/.

But somehow it comes to my mind, what if we use PUT method?


Bharat Mehta
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41101
    
  45
If you just want a file name, you can make the input field of type "text", and have the user enter the file name. Also, I would question the request not to use 3rd party libraries. It's a lot of work to recreate a file upload handling library from scratch; what would be the point of that?

what if we use PUT method?
HTML forms do not support PUT (and I'm not sure how that would help, either).

Ping & DNS - my free Android networking tools app
Frank Eichfelder
Ranch Hand

Joined: Aug 25, 2003
Posts: 33
Have you tried to remove enctype="multipart/form-data" from the form tag?
I think you should receive the filename, if you omit the enctype-attribute.
Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Pardon me folks if I am wrong here, as I have just started to take dive in the waters of Servlets.

But I tried with PUT method.... and it did worked. Here is a summary of what I did...


and the Servlet code where i override the doPut method goes like this:


Any comments/thoughts on this?
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
Thanks All,

But my problem still persists.
Question to Bharat : Even i tried the same (to be frank : just copy paste i have done )after you, but the control is not going to my servlet atall. Simply i am getting

The page cannot be displayed
The page you are looking for cannot be displayed because the address is incorrect.


After you submit the form how the url should change?

Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Srinivas , the reason for you getting a "Page can not be displayed" may be some conflicts between what you specified at "action" in your <form> and <url-pattern> in the Deployment Descriptor.

May be you are missing something there. Please check this part . Hope this helps you. And one thing more why have you used a "/" in beginning of <code>action="/FileTransfer/TempServlet"</code>?
Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
But its working very fine without the enctype attribut , however i am getting the same error inspite of using <form action="TempServlet" method="PUT" enctype="multipart/form-data">
Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
How about if we do this like below:


since you had used FileTransfer earlier.? It will be a guess since I have no sight of you Deployment Descriptor and the mappings therein

Srinivasa Maddi
Ranch Hand

Joined: Apr 18, 2007
Posts: 60
No it dint work. See my dd
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41101
    
  45
Bharat Mehta wrote:But I tried with PUT method.... and it did worked.
...
Any comments/thoughts on this?

As I said, PUT is not supported by HTML forms. See http://www.w3.org/TR/html401/interact/forms.html#submit-format. What happens if you use it is undefined, and likely to be dependent on the browser.
Bharat Mehta
Greenhorn

Joined: Oct 29, 2007
Posts: 8
Ok I got you point Ulf . We should not be using 'PUT' here. Thanks
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
It´s fine that you only need the file name and not the file contents, but realize that you can´t get the file contents in any way by only the file name. Maybe only if both the client and the server runs at physically the same machine, but certainly not another than that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.getParameter("fileName") returning null in post method of servlet . Why?
 
Similar Threads
GET Method Working But POST is not
user authentication and dao
dopost within doget
Java Dynamic Servlet
Cookie access with EL expression