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

时间:2010-08-26 16:49:35

标签: java servlets http-status-code-405

当我使用下面的代码(下面)时,我收到错误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。如何在不收到错误的情况下进行此推荐?提前谢谢。

2 个答案:

答案 0 :(得分:8)

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


显然,URL /EditObject映射到另一个没有doPost()方法覆盖的servlet上。它也会在RequestDispatcher#forward()上调用,因为当前运行HTTP请求的方法是POST。 默认 HttpServlet#doPost()实现将返回HTTP 405.如果您的实际意图是在其上发出GET请求以便调用doGet()方法,那么您应该使用而是HttpServletResponse#sendRedirect()

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

答案 1 :(得分:2)

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

    }
}