File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes HTTP method POST 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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP method POST is not supported by this url" Watch "HTTP method POST is not supported by this url" New topic
Author

HTTP method POST is not supported by this url

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I am trying to run the example in chapter 3 of the Heads First Servlets and JSP book. Anyone know why I am getting it and any suggestions on how to fix it.

THanks in advance.

Sonia
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Maybe its happening because you don't have a doGet or doPost in your servlet and you are calling that method.
Really can't say without looking at your code. You might consider posting some?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I dont know if it is legal to post the code. Let me read the book to see if I can reuse the code. But in the meantime I do have a doPost method
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

It's legal to post one line of code as an example. My guess would be that you might have a doPost method, but that it doesn't override doPost as it should. Instead it overloads it, perhaps with a different parameter list.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

And the import statements for the classes referred to in that statement?
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

Are you sure you are calling the correct servlet from your html/jsp page?
Are you running the code straight of a CD or are you typing the code yourself to run it?

Again really difficult to say what is going wrong - without looking at the code. Maybe if you tell us what is the example you are trying out - someone could help you develop one from scratch. That way you'll learn more and have your own copy righted code

P.S - I really don't think code in a book can be copy righted - especially the small snippets - they are usually too trivial. Not 100% - Just wondering actually - maybe someone could confirm?
raghu yeggina
Greenhorn

Joined: Feb 11, 2009
Posts: 27
hi

first of all did you specify <form action="login" method="post" > in your html/jsp?

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I was able to fix it.

This is what I did. I changed the get method to a post method in my servlet.
Then in the same servlet I coded the post method:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

I don't understand why I needed both the methods for the page to work. Can
someone explain?

Thanks,

Veena
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874





public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}

don't understand why I needed both the methods for the page to work. Can
someone explain?


Well you dont need it. Each servlet that you write , you would be able to determine to handle POST or GET requests ,
depending on that you can override the methods. but since its not possible to control the user from hitting the servlet
directly( which is GET by default ) , so it would be better to declare both the methods and forward the real work to any one
of the method as pointed in the code above.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
How is that possible that it can hit GET by default when I have configured into the
form.html the POST method. The form.html is the form I load first and it has POST configured in it and an action that loads the results.jsp page.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


. The form.html is the form I load first and it has POST configured in it and an action that loads the results.jsp page.


What if you dont load that form first , but hit directly the action URL it has ( by typing in the url ) .. ofcourse this is not the real case though...

As said in your case if you defined as POST method in form.html , it will always call doPost(); and no need of doGet()
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
ok, so I am back to square 1. I don't know why it is complaining when I just have doPost method in my servlet.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


This is what I did. I changed the get method to a post method in my servlet.
Then in the same servlet I coded the post method:


Please take a look at Chapter 4 of HF Servlet and Java and understand the basics of Post and GET and when to use one.

To answer your query , did you tried it the way you asked the question ?
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I have read it. doesn't answer my question. i have tried it with both methods and if you read my previous posts it says it works with both methods overridden
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

So you are saying that when you override doPost() it doesn't work when you make a POST request unless you also override doGet()?

That sounds mighty specious.

Since you are worried about posting book code, just write up your own simple servlet that demonstrates the issue and post it here.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
Here is the servlet code;


Here is my form.html that i type in my url:

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
GET requests on a servlet invokes doGet().
POST requests on a servlet invokes doPost().

You're calling doGet() inside your doPost().

Think twice about it.

Your doGet() method content belongs in the doPost().
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Without code tags, the code is unreadable. Please read this for more information. You can go back and change your post to add code tags by clicking the button on your post.

Also, post the code that isn't working, not the hacked-up version.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

sonia pandit wrote:Here is the servlet code;


Here is my form.html that i type in my url:



And the problem you face with this code is? (expect for the fact that i won't compile without the equal to sign after String c?)
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
The problem I am having is I get this message: Http Post is not supported by this url
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Your form action is wrong. It should not be the servlet-name, but the url-pattern.

If that doesn't solve the problem, then your classpath is probably a mess. You've another (older) class version which does not implement doPost() somewhere else in the classpath which got precedence in classloading. For example jdk/lib, appserver/lib, etc. Clean up your classpath.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Not very sure but your server might be blocking post requests, try this once

<servlet-mapping>
what ever you have
<http-method>POST</http-method>
</sevlet-mapping>


Swastik
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Swastik Dey wrote:Not very sure but your server might be blocking post requests, try this.../quote]
Before adding goop to the web.xml, let's get the action URL and classpath settled.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTTP method POST is not supported by this url