HTTP状态405-此URL不支持HTTP方法POST

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();

}

}

}

编辑:我throws ServletException, IOException按照建议添加了,但这并没有改变错误。

编辑: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);

}

}

另一个编辑:所以基本上我不能做我正在做的事情。我需要的是,用户提交一个发布请求,然后将他/她转回到使用Get方法而不是Post的servlet。如何在不出现错误的情况下进行此引荐?提前致谢。

回答:

(很抱歉,我之前发布了错误的答案,因此将其删除)


显然,URL

/EditObject被映射到另一个没有doPost()覆盖方法的servlet

。也将调用它RequestDispatcher#forward(),因为当前运行的HTTP请求的方法是POST。该

默认HttpServlet#doPost()实现将返回HTTP

405如果您的实际意图是火中的GET请求,这样的doGet()方法将被调用,那么你应该宁愿使用HttpServletResponse#sendRedirect()来代替。

response.sendRedirect("/EditObject?id="+objId);

以上是 HTTP状态405-此URL不支持HTTP方法POST 的全部内容, 来源链接: utcz.com/qa/435898.html

回到顶部