4

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。如何在不收到错误的情况下进行此推荐?提前致谢。

4

2 回答 2

8

(对不起我之前发布的错误答案,我删除了它)


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

response.sendRedirect("/EditObject?id="+objId);
于 2010-08-26T17:03:10.547 回答
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);

    }
}
于 2010-08-26T17:11:10.773 回答