This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm writing a basic javaservlet which uploads files to an 'uploads' folder on my local disk (I'm on mac OSx10). There seems to be an issue with the POST request in the form, when I implement a doPost method in the servlet. I know that you can override the methods to accept either get/post but I assume I shouldn't need to do that as I use the Post action in the client form. When compiled I get the error message:
HTTP Status 405 - HTTP method GET is not supported by this URL
Here is the code (sorry couldn't see an option to tag as HTML code?):
So my questions are:
1) Why does form method get and servlet post method compile okay but not post and post?
2) Is there anything obvious I am missing which is causing the file upload to fail with get/post - I've checked the folder access is okay for where I am trying to write the file to but it doesn't seem to throw that exception anyway??
Thanks for your help in advance, I am going round in circles here! I know this will be something stupid I have done/missed.
You are not forced to declare any of doGet or doPost methods. That depends on how you are planning to use that servlet.
If you want your servlets to support HTTP Get method (eg when user directly hit the url in browser) implement doGet() or you will get that error.
If you want your servlet to just support http post mehod (eg a form with method=post), you don't need doGet and you can declare just doPost method.
i hope you get the point :-
- If no doGet() - trying to access the servlet using http get method would result in 405
- if no doPost() - HTTP post request would result in 405
Joined: Mar 22, 2011
I fixed the issue, thanks for clarification on doGet & doPost - it is as I thought. The only thing I can see that caused the issues is trying to output html in FileUploadException e in the catch blocks.