aspose file tools*
The moose likes Servlets and the fly likes HTTP Status 405 - HTTP method GET is not supported by this URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP Status 405 - HTTP method GET is not supported by this URL" Watch "HTTP Status 405 - HTTP method GET is not supported by this URL" New topic
Author

HTTP Status 405 - HTTP method GET is not supported by this URL

Paul Featherstone
Greenhorn

Joined: Mar 22, 2011
Posts: 6
Hi,

I'm writing a basic java servlet 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?):

Form:

<form enctype="multipart/form-data“ method="POST" action="FileUploadServlet">
<input type="file" name="file"><br /> <br /> <input type="submit" value="upload the file">
</form>

Servlet:



If I change the form method in the form to Get then the code compiles okay e.g. http://localhost:8080/CMS/FileUploadServlet?file=atextfile.txt but I get the "Failed to upload file. Sorry!" exception. I'm just getting used to debugging - hasn't told me much so far I'm afraid.

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.

Regards,
Paul
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

see this thread http://www.coderanch.com/t/529961/Servlets/java/Getting-Error-HTTP-Status-HTTP

[Servlet tutorial] [Servlet 3.0 Cook Book]
Paul Featherstone
Greenhorn

Joined: Mar 22, 2011
Posts: 6
Hi Sudhir,

Thanks for the quick response.

So, in this servlet you can't only have a doPost method, a doGet method is needed too. That makes sense, page request and form submission.

Tried again and white screen, still seems to be an issue and no error message this time??!

Paul
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

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
Paul Featherstone
Greenhorn

Joined: Mar 22, 2011
Posts: 6
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.

cheers
paul
 
Consider Paul's rocket mass heater.
 
subject: HTTP Status 405 - HTTP method GET is not supported by this URL