• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Srinivasa Maddi
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just file name and moreover here i was asked not to go with any third party jars.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it dint work. See my dd
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got you point Ulf . We should not be using 'PUT' here. Thanks
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic