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

itz*_*yjr 6 servlets http-get http-status-code-405

下面的代码来自一本书,所以它不会出错.但我不知道如何解决这个错误.当删除方法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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Sil*_*ito 13

我刚才遇到了同样的问题."HTTP状态405 - 此URL不支持HTTP方法GET".我的解决方案如下:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的构造函数有问题,因为"doGet"我称之为超级

  • 这个引起了我的注意 - 我认为在重写方法时,调用父方法是标准做法.调用super.doGet(req,resp)导致此错误.(让我抓狂,因为我的doGet中的断点被击中......然后服务器会响应405 ......尽管我明确地设置了状态) (8认同)

Har*_*hra 10

Servlet代码似乎是正确的.提供web.xml条目和Servlet调用URL.

导致此错误的主要原因有两个:

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

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}
Run Code Online (Sandbox Code Playgroud)

2)您从HTML表单发出了HTTP post请求,但是没有doPost()方法来处理它.doGet()无法处理"发布"请求.

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
....
}
Run Code Online (Sandbox Code Playgroud)

阅读@ BalusC的答案以获取更多详细信息.:Servlet中的doGet和doPost