2

I have searched a lot on this topic, but no questions/answers seem to provide what I am looking for. I apologize if the answer is floating around out there somewhere.

What I have are a series of utility JSPs that I have written, that I accessing via an index type JSP where I have a button form to go to the appropriate JSP. each one looks like this:

<form method="POST" action="delete.jsp">
<input type="submit" value="Delete" name="B2">
</form>

in my delete.jsp, I have another form that calls my servlet once the button is pressed, like this:

<form method="post" action="ModelServlet">
Select file to delete: <input type="file" name="dataFile" id="fileChooser" /><br />
<br /> <input type="submit" name="DELETE" value="Delete" />
</form>

When I press this 'delete' button, I get an "HTTP Status 405 - HTTP method POST is not supported by this URL" error. This leads me to believe there is some kind of mapping error, or something, preventing the utility JSP from finding my servlet. Any ideas on this? my servlet code is below:

my servlet that it is calling:

package com.model;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;

public class ModelServlet extends HttpServlet {
        protected void init(){}

    protected void doPost(Model ourModel, HttpServletRequest   request, HttpServletResponse response) throws ServletException, IOException {
        //check for which case we have: upload, delete, move, or copy
        if (ServletFileUpload.isMultipartContent(request)){
        //do upload stuff
        performUpload(request,response);            
    }
    else if (request.getParameter("DELETE") != null){
        //do delete stuff
        performDelete(request,response);
    }
    else if (request.getParameter("MOVE") != null){
        //do move stuff
        performMove(request,response);

    }
    else if (request.getParameter("COPY")!= null){
        //do copy stuff
        performCopy(request,response);
    }

}

with methods defined for the functions below it.

Cheers, Joe

Joe McG
  • 137
  • 1
  • 2
  • 9

1 Answers1

2

You didn't interpret the error rightly. It found the servlet, but not the desired method. As you're using <form method="post">, it's obviously looking for the doPost() method. So you just need to make sure that you've provided one (and that you don't incorrectly delegate to super.doPost()). Make use of the @Override annotation on the method to ensure that you indeed overrode the right one and thus didn't typo'ed the method signature which made it to be an overloaded (but not actually used) one.

If it didn't found the whole servlet, you would have gotten a 404 error instead.


Update: as per your question update, your servlet isn't overidding the real doPost() method at all. There's an unknown third argument which makes it effectively an overloaded method which is not specified by the servlet API.

protected void doPost(Model ourModel, HttpServletRequest   request, HttpServletResponse response) throws ServletException, IOException {

If you put the @Override annotation on the method, you'll see that it won't compile at all because of the invalid Model ourModel argument. Fix it accordingly:

@Override
protected void doPost(HttpServletRequest  request, HttpServletResponse response) throws ServletException, IOException {
Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • I guess I should have researched the API a little more in depth! your answer worked. Thank you BalusC, you are a huge help. – Joe McG Feb 04 '13 at 21:52