4

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

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.

EDIT: the EditObject servlet looks like this

 @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.

Ankur
  • 47,089
  • 107
  • 237
  • 309
  • Where's the servlet that handles /EditObject ? – nos Aug 26 '10 at 17:03
  • @Ankur: which class or servlet handling the /EditObject url? – mhshams Aug 26 '10 at 17:03
  • You are trying to change a POST method into another GET. The way you are doing it is wrong. The request dispatcher does not look at the query string. Whatever is mapped under /EditObject is not able to deal with POST method. – gawi Aug 26 '10 at 17:07
  • @mohammad ... I added some detail, can add more if required. Thanks – Ankur Aug 26 '10 at 17:08
  • @Ankur: you need to change your doGet method in EditObject to doPost. not sure if its affect rest of your code or not. – mhshams Aug 26 '10 at 17:10
  • @mohammad - it will change things. I want the user to be able to see the querystring, because the expert users will sometimes benefit from seeing the numeric id (objId) value. Is there any way around this. It doesn't seem to make sense to me that the two should be tied together. Each request is separate ... or so I would have thought. – Ankur Aug 26 '10 at 17:12
  • @Ankur: did you *read* my answer? I suggested to use `HttpServletResponse#sendRedirect()` instead. – BalusC Aug 26 '10 at 17:13

2 Answers2

8

(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 default HttpServlet#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.

response.sendRedirect("/EditObject?id="+objId);
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
2

Add a doPost() to your EditObject class:

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

    }
}
gawi
  • 13,050
  • 7
  • 38
  • 74
  • ...you should rename "id" parameter to "objId" everywhere in order for this to work. – gawi Aug 26 '10 at 17:13
  • 1
    Renaming parameter is not needed. It's passed with name `id` and retrieved with name `id`. Only the variablename is indeed not consistent with it. I would however agree to get rid of that hungarian-like notation. It's ugly. – BalusC Aug 26 '10 at 17:15
  • THanks, I can see that this would be a good technique as well. – Ankur Aug 26 '10 at 17:18
  • In the original POST request, parameter name is "objId" while "id" is expected in EditObject class. The "id" parameter supply in the getServletContext().getRequestDispatcher("/EditObject?id="+objId) call is superfluous as it is not used to perform dispatch. getServletContext().getRequestDispatcher("/EditObject") is enough. – gawi Aug 26 '10 at 17:21