HTTP状态码405 - 此URL不支持HTTP GET方法。

6
以下代码来自一本书,因此不会出错。但是我不知道如何解决下面的错误。删除doGet()方法后,仍然会出现相同的错误!
"HTTP状态405 - 此URL不支持HTTP GET方法"
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override 
protected void doGet(HttpServletRequest request,HttpServletResponse response) 
throws IOException,ServletException{
    this.doPost(request,response);
}
@Override 
protected void doPost(HttpServletRequest request,HttpServletResponse response) 
                                   throws IOException,ServletException{
    response.setContentType("application/pdf");
    ServletOutputStream out=response.getOutputStream();
    File pdf=null;
    BufferedInputStream buf=null;
    try{
        pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");
        response.setContentLength((int)pdf.length());
        FileInputStream input=new FileInputStream(pdf);
        buf=new BufferedInputStream(input);
        int readBytes=0;
        while((readBytes=buf.read())!=-1)    out.write(readBytes);
    }catch(IOException e){
        System.out.println("file not found!");
    }finally{
        if(out!=null) out.close();
        if(buf!=null) buf.close();
    }
}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
-<web-app xsi:.........." version="2.5"> 
-<servlet> 
<description>This is the description of my Java EE component</description> 
<display-name>This is the display name of my Java EE component</display-name> 
<servlet-name>PDFServlet</servlet-name> 
<servlet-class>PDFServlet</servlet-class> 
</servlet> 
-<servlet-mapping> 
<servlet-name>PDFServlet</servlet-name> 
<url-pattern>/PDFServlet</url-pattern> 
</servlet-mapping> 
-<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
-<login-config> 
<auth-method>BASIC</auth-method> 
</login-config> 
</web-app>
7个回答

14

我刚刚也遇到了这个问题。"HTTP状态405 - HTTP方法GET不受此URL支持"。我的解决方法如下:

public abstract class Servlet extends HttpServlet {

    protected HttpServletRequest req;
    protected HttpServletResponse resp;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.req = req;
        this.resp = resp;
        this.requestManager();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.req = req;
        this.resp = resp;
        this.requestManager();

    }

    protected abstract void requestManager() throws IOException;
}

我在构造函数中遇到了问题,因为我调用了"doGet"时使用了super。


8
这是让我困惑的地方——我曾认为当覆盖一个方法时,标准做法是调用父类的方法。调用super.doGet(req, resp) 导致了这个错误。(让我抓狂了,因为我的doGet中设置了断点,但服务器仍然响应了405状态码...尽管我明确地设置了状态码)。 - wwwhack

12

Servlet代码似乎是正确的。请提供web.xml入口和Servlet调用的URL。

引起此错误的主要原因有两个:

1)您没有有效的doGet()方法,当您直接在地址栏中输入servlet的路径时,像Tomcat这样的Web容器将尝试调用doGet()方法。

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}

2) 你从 HTML 表单中发起了一个 HTTP POST 请求,但是你没有一个 doPost() 方法来处理它。而 doGet() 方法无法处理这个“POST”请求。

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}

更多细节请参阅@BalusC的答案:Servlets中的doGet和doPost


Servlet调用URL:“http://localhost:8080/Test/PDFServlet”(Test:项目名称) - itzyjr
现在我不知道,但出现了一个新问题:在我的MyEclipse窗口中,输出总是“文件未找到!”,为什么?我的文件路径是正确的。 - itzyjr
我解决了这个问题,非常感谢!最终,这是关于服务器的问题,我会处理好的。 - itzyjr
很高兴听到问题已经解决。不客气。 - Hardik Mishra
@itzyjr 那问题是什么? - eis

-1
当出现上述错误时,请重写doGet()方法。
@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp); //To change body of generated methods, choose Tools | Templates.
    }

-1

你需要做的是

<form action="servlet name " method="post">

在你的index.jsp文件中


-1

我正在使用HTML文件来创建网页。当我遇到这个错误时,我的解决方案是:只需在我的web.xml文件中删除“index.html”路径,因为我的HTML文件名与“index.html”相同。


-1
替换这行代码
pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");

使用

pdf=new File("C:/Users/lk/Desktop/Desktop/example.pdf");

然后再继续进行。


-3
每个Servlet必须包含一个doGet()方法,该方法默认由服务器执行。因此,请确保您有doGet方法。

1
不,他们不需要这样做。如果您计划使用标准servlet接口支持GET请求,则是,但您不必这样做。 - eis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接