aspose 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 Java 8 in Action this week in the Java 8 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

Sudhir V
Ranch Hand

Joined: Dec 25, 2002
Posts: 143
Hi
I have made a form with "post" request
<form action="servlet/form1_servlet" method="post">
State: <select name="state" size=3 multiple>
<option value="NJ">New Jersey</option>
<option value="OK">Oklahoma</option>
</select>
<input type="submit" value="click">
</form>
and the action specifies the servlet below. I am getting this error "HTTP method POST is not supported by this URL" when I click the button in the page. however the servlet works fine when I change the request to "get".
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class form1_servlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<body>");
String val = req.getParameter("state");
pw.println(val);
pw.println("</body>");
pw.println("</html>");
}
}
Thanks
Sudhir


Sudhir V<br />(SCJP 1.2, SCWCD, OCA, SCBCD)
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
You need to implement the doPost() method in your servlet.
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
hey,
if you want your servlet to do the same thing when it gets both post and get requests
the best practice is not to implement service(), but to implement doGet(request,response)
and to implement doPost the next way:
doPost(request,response)
{
doGet(request,response);
}
this way you cover both doPost and doGet


Asher Tarnopolski
SCJP,SCWCD
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
Actually I'd rather re-route both doGet() and doPost() to a third method called "doService()" or something like that just in case you decide you want to change the behavior of one or the other in the future.
However, this still doesn't explain why sudhir's implementation doesn't work -- HttpServlet implements service(HttpServletRequest,HttpServletResponse) by calling doGet() and doPost(). By overriding it in his method, he should have short-circuited this behavior.
Sudhir, is this a copy-cut-and-paste of your exact code, or did you rewrite it in your post? Perhaps you made a spelling error in the name of the method?
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
yeh, kyle is right, the code must work.
so, may be the problem is the deployment?
where do you put your html anf the servlet files?
did u try to make the form target begin with "/"?
 
Don't get me started about those stupid light bulbs.
 
subject: HTTP method POST is not supported by this URL
 
Similar Threads
Problem running a sample Servlet Example: HTTP 503 Status code
Question regarding: servlet-mapping
getting 500 error when trying to run a servlet
Question regarding servlet-mapping in web.xml
can anybody help me