Java HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3577414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Status 405 - HTTP method POST is not supported by this URL
提问by Ankur
I am getting the error HTTP Status 405 - HTTP method POST is not supported by this URL
when I use the following code(below) ... the line causing the trouble (apparently) is getServletContext().getRequestDispatcher("/EditObject?id="+objId).forward(request, response);
HTTP Status 405 - HTTP method POST is not supported by this URL
当我使用以下代码(如下)时出现错误......导致问题的行(显然)是getServletContext().getRequestDispatcher("/EditObject?id="+objId).forward(request, response);
package web.objects;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ObjDetailsDao;
@SuppressWarnings("serial")
public class EditObjectText extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int objId = Integer.parseInt(request.getParameter("objId"));
String text = (String)request.getParameter("description");
ObjDetailsDao oddao = new ObjDetailsDao();
try {
oddao.modifyText(text, objId);
/////////////
getServletContext().getRequestDispatcher("/EditObject?id="+objId).forward(request, response);
////////////
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
EDIT: I added the throws ServletException, IOException
as suggested, but this did not change the error.
编辑:我throws ServletException, IOException
按照建议添加了,但这并没有改变错误。
EDIT: the EditObject servlet looks like this
编辑:EditObject servlet 看起来像这样
@SuppressWarnings("serial")
public class EditObject extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
int objId = Integer.parseInt(request.getParameter("id"));
dispPage(objId, request, response);
}
private void dispPage(int objId, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// ... lots of code in here
getServletContext().getRequestDispatcher("/jsp/objectPageEdit.jsp").forward(request, response);
}
}
ANOTHER EDIT: So basically I can't do what I am doing. What I need is this, the user submits a post request and then I refer him/her back to a servlet which uses the Get method instead of Post. How can I do this referral without getting the error? Thanks in advance.
另一个编辑:所以基本上我不能做我正在做的事情。我需要的是,用户提交一个发布请求,然后我将他/她推荐回使用 Get 方法而不是 Post 的 servlet。如何在不出错的情况下进行此推荐?提前致谢。
采纳答案by BalusC
(sorry about the wrong answer I posted before, I deleted it).
(抱歉之前贴错了答案,我删除了)。
Apparently the URL /EditObject
is mapped on another servlet which doesn't have doPost()
method overriden. It would be called on RequestDispatcher#forward()
as well because the method of currently running HTTP request is POST. The defaultHttpServlet#doPost()
implementation will return HTTP 405. If your actual intent is to fire a GET request on it so that the doGet()
method will be invoked, then you should rather use HttpServletResponse#sendRedirect()
instead.
显然,该 URL/EditObject
映射到另一个没有doPost()
覆盖方法的servlet 。它也会被调用,RequestDispatcher#forward()
因为当前运行 HTTP 请求的方法是 POST。该默认HttpServlet#doPost()
实现将返回HTTP 405如果您的实际意图是火中的GET请求,这样的doGet()
方法将被调用,那么你应该宁愿使用HttpServletResponse#sendRedirect()
来代替。
response.sendRedirect("/EditObject?id="+objId);
回答by gawi
Add a doPost() to your EditObject class:
将 doPost() 添加到您的 EditObject 类:
@SuppressWarnings("serial")
public class EditObject extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
process(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
process(request, response);
}
public void process(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
int objId = Integer.parseInt(request.getParameter("id"));
dispPage(objId, request, response);
}
private void dispPage(int objId, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// ... lots of code in here
getServletContext().getRequestDispatcher("/jsp/objectPageEdit.jsp").forward(request, response);
}
}