• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTP method POST is not supported by this URL

 
Sudhir V
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to implement the doPost() method in your servlet.
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 "/"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic